jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OpenGGSN - Gateway GPRS Support Node |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2002, 2003, 2004 Mondru AB. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 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 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * gtp.c: Contains all GTP functionality. Should be able to handle multiple |
| 14 | * tunnels in the same program. |
| 15 | * |
| 16 | * TODO: |
| 17 | * - Do we need to handle fragmentation? |
| 18 | */ |
| 19 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 20 | #ifdef __linux__ |
| 21 | #define _GNU_SOURCE 1 |
| 22 | #endif |
| 23 | |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 24 | #include "../config.h" |
| 25 | #ifdef HAVE_STDINT_H |
| 26 | #include <stdint.h> |
| 27 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 28 | |
| 29 | #include <syslog.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdarg.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <netinet/in.h> |
| 37 | #include <arpa/inet.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <time.h> |
| 40 | #include <unistd.h> |
| 41 | #include <string.h> |
| 42 | #include <errno.h> |
| 43 | #include <fcntl.h> |
| 44 | |
| 45 | #include <arpa/inet.h> |
| 46 | |
jjako | bae2cd4 | 2004-01-09 12:04:39 +0000 | [diff] [blame] | 47 | /* #include <stdint.h> ISO C99 types */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 48 | |
| 49 | #include "pdp.h" |
| 50 | #include "gtp.h" |
| 51 | #include "gtpie.h" |
| 52 | #include "queue.h" |
| 53 | |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 54 | /* According to section 14.2 of 3GPP TS 29.006 version 6.9.0 */ |
| 55 | #define N3_REQUESTS 5 |
| 56 | |
| 57 | #define T3_REQUEST 3 |
| 58 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 59 | /* Error reporting functions */ |
| 60 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 61 | void gtp_err(int priority, char *filename, int linenum, char *fmt, ...) |
| 62 | { |
| 63 | va_list args; |
| 64 | char buf[ERRMSG_SIZE]; |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 65 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 66 | va_start(args, fmt); |
| 67 | vsnprintf(buf, ERRMSG_SIZE, fmt, args); |
| 68 | va_end(args); |
| 69 | buf[ERRMSG_SIZE - 1] = 0; |
| 70 | syslog(priority, "%s: %d: %s", filename, linenum, buf); |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void gtp_errpack(int pri, char *fn, int ln, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 74 | void *pack, unsigned len, char *fmt, ...) |
| 75 | { |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 76 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 77 | va_list args; |
| 78 | char buf[ERRMSG_SIZE]; |
| 79 | char buf2[ERRMSG_SIZE]; |
| 80 | unsigned int n; |
| 81 | int pos; |
| 82 | |
| 83 | va_start(args, fmt); |
| 84 | vsnprintf(buf, ERRMSG_SIZE, fmt, args); |
| 85 | va_end(args); |
| 86 | buf[ERRMSG_SIZE - 1] = 0; |
| 87 | |
| 88 | snprintf(buf2, ERRMSG_SIZE, "Packet from %s:%u, length: %d, content:", |
| 89 | inet_ntoa(peer->sin_addr), ntohs(peer->sin_port), len); |
| 90 | buf2[ERRMSG_SIZE - 1] = 0; |
| 91 | pos = strlen(buf2); |
| 92 | for (n = 0; n < len; n++) { |
| 93 | if ((pos + 4) < ERRMSG_SIZE) { |
| 94 | sprintf((buf2 + pos), " %02hhx", |
| 95 | ((unsigned char *)pack)[n]); |
| 96 | pos += 3; |
| 97 | } |
| 98 | } |
| 99 | buf2[pos] = 0; |
| 100 | |
| 101 | syslog(pri, "%s: %d: %s. %s", fn, ln, buf, buf2); |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 102 | |
| 103 | } |
| 104 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 105 | /* API Functions */ |
| 106 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 107 | const char *gtp_version() |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 108 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 109 | return VERSION; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* gtp_new */ |
| 113 | /* gtp_free */ |
| 114 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 115 | int gtp_newpdp(struct gsn_t *gsn, struct pdp_t **pdp, |
| 116 | uint64_t imsi, uint8_t nsapi) |
| 117 | { |
| 118 | return pdp_newpdp(pdp, imsi, nsapi, NULL); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 121 | int gtp_freepdp(struct gsn_t *gsn, struct pdp_t *pdp) |
| 122 | { |
| 123 | return pdp_freepdp(pdp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 124 | } |
| 125 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 126 | /* gtp_gpdu */ |
| 127 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 128 | extern int gtp_fd(struct gsn_t *gsn) |
| 129 | { |
| 130 | return gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* gtp_decaps */ |
| 134 | /* gtp_retrans */ |
| 135 | /* gtp_retranstimeout */ |
| 136 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 137 | int gtp_set_cb_unsup_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 138 | int (*cb) (struct sockaddr_in * peer)) |
| 139 | { |
| 140 | gsn->cb_unsup_ind = cb; |
| 141 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 142 | } |
| 143 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 144 | int gtp_set_cb_extheader_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 145 | int (*cb) (struct sockaddr_in * peer)) |
| 146 | { |
| 147 | gsn->cb_extheader_ind = cb; |
| 148 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 149 | } |
| 150 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 151 | /* API: Initialise delete context callback */ |
| 152 | /* Called whenever a pdp context is deleted for any reason */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 153 | int gtp_set_cb_delete_context(struct gsn_t *gsn, int (*cb) (struct pdp_t * pdp)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 154 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 155 | gsn->cb_delete_context = cb; |
| 156 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 157 | } |
| 158 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 159 | int gtp_set_cb_conf(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 160 | int (*cb) (int type, int cause, |
| 161 | struct pdp_t * pdp, void *cbp)) |
| 162 | { |
| 163 | gsn->cb_conf = cb; |
| 164 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Harald Welte | 629e986 | 2010-12-24 20:58:09 +0100 | [diff] [blame] | 167 | int gtp_set_cb_recovery(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 168 | int (*cb) (struct sockaddr_in * peer, uint8_t recovery)) |
| 169 | { |
| 170 | gsn->cb_recovery = cb; |
| 171 | return 0; |
Harald Welte | 629e986 | 2010-12-24 20:58:09 +0100 | [diff] [blame] | 172 | } |
| 173 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 174 | extern int gtp_set_cb_data_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 175 | int (*cb_data_ind) (struct pdp_t * pdp, |
| 176 | void *pack, unsigned len)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 177 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 178 | gsn->cb_data_ind = cb_data_ind; |
| 179 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 180 | } |
| 181 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 182 | /** |
| 183 | * get_default_gtp() |
| 184 | * Generate a GPRS Tunneling Protocol signalling packet header, depending |
| 185 | * on GTP version and message type. pdp is used for teid/flow label. |
| 186 | * *packet must be allocated by the calling function, and be large enough |
| 187 | * to hold the packet header. |
| 188 | * returns the length of the header. 0 on error. |
| 189 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 190 | static unsigned int get_default_gtp(int version, uint8_t type, void *packet) |
| 191 | { |
| 192 | struct gtp0_header *gtp0_default = (struct gtp0_header *)packet; |
| 193 | struct gtp1_header_long *gtp1_default = |
| 194 | (struct gtp1_header_long *)packet; |
| 195 | switch (version) { |
| 196 | case 0: |
| 197 | /* Initialise "standard" GTP0 header */ |
| 198 | memset(gtp0_default, 0, sizeof(struct gtp0_header)); |
| 199 | gtp0_default->flags = 0x1e; |
| 200 | gtp0_default->type = hton8(type); |
| 201 | gtp0_default->spare1 = 0xff; |
| 202 | gtp0_default->spare2 = 0xff; |
| 203 | gtp0_default->spare3 = 0xff; |
| 204 | gtp0_default->number = 0xff; |
| 205 | return GTP0_HEADER_SIZE; |
| 206 | case 1: |
| 207 | /* Initialise "standard" GTP1 header */ |
| 208 | /* 29.060: 8.2: S=1 and PN=0 */ |
| 209 | /* 29.060 9.3.1: For GTP-U messages Echo Request, Echo Response */ |
| 210 | /* and Supported Extension Headers Notification, the S field shall be */ |
| 211 | /* set to 1 */ |
| 212 | /* Currently extension headers are not supported */ |
| 213 | memset(gtp1_default, 0, sizeof(struct gtp1_header_long)); |
| 214 | gtp1_default->flags = 0x32; /* No extension, enable sequence, no N-PDU */ |
| 215 | gtp1_default->type = hton8(type); |
| 216 | return GTP1_HEADER_SIZE_LONG; |
| 217 | default: |
| 218 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 219 | "Unknown GTP packet version"); |
| 220 | return 0; |
| 221 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 222 | } |
| 223 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 224 | /** |
| 225 | * get_seq() |
| 226 | * Get sequence number of a packet. |
| 227 | * Returns 0 on error |
| 228 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 229 | static uint16_t get_seq(void *pack) |
| 230 | { |
| 231 | union gtp_packet *packet = (union gtp_packet *)pack; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 232 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 233 | if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */ |
| 234 | return ntoh16(packet->gtp0.h.seq); |
| 235 | } else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */ |
| 236 | return ntoh16(packet->gtp1l.h.seq); |
| 237 | } else { |
| 238 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag"); |
| 239 | return 0; |
| 240 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
| 244 | * get_tid() |
| 245 | * Get tunnel identifier of a packet. |
| 246 | * Returns 0 on error |
| 247 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 248 | static uint64_t get_tid(void *pack) |
| 249 | { |
| 250 | union gtp_packet *packet = (union gtp_packet *)pack; |
| 251 | |
| 252 | if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */ |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 253 | return be64toh(packet->gtp0.h.tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 254 | } |
| 255 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
| 259 | * get_hlen() |
| 260 | * Get the header length of a packet. |
| 261 | * Returns 0 on error |
| 262 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 263 | static uint16_t get_hlen(void *pack) |
| 264 | { |
| 265 | union gtp_packet *packet = (union gtp_packet *)pack; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 266 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 267 | if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */ |
| 268 | return GTP0_HEADER_SIZE; |
| 269 | } else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */ |
| 270 | return GTP1_HEADER_SIZE_LONG; |
| 271 | } else if ((packet->flags & 0xe7) == 0x20) { /* Short version 1 */ |
| 272 | return GTP1_HEADER_SIZE_SHORT; |
| 273 | } else { |
| 274 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag"); |
| 275 | return 0; |
| 276 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
| 280 | * get_tei() |
| 281 | * Get the tunnel endpoint identifier (flow label) of a packet. |
| 282 | * Returns 0xffffffff on error. |
| 283 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 284 | static uint32_t get_tei(void *pack) |
| 285 | { |
| 286 | union gtp_packet *packet = (union gtp_packet *)pack; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 287 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 288 | if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */ |
| 289 | return ntoh16(packet->gtp0.h.flow); |
| 290 | } else if ((packet->flags & 0xe0) == 0x20) { /* Version 1 */ |
| 291 | return ntoh32(packet->gtp1l.h.tei); |
| 292 | } else { |
| 293 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag"); |
| 294 | return 0xffffffff; |
| 295 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 296 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 297 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 298 | int print_packet(void *packet, unsigned len) |
| 299 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 300 | unsigned int i; |
| 301 | printf("The packet looks like this (%d bytes):\n", len); |
| 302 | for (i = 0; i < len; i++) { |
| 303 | printf("%02x ", (unsigned char)*(char *)(packet + i)); |
| 304 | if (!((i + 1) % 16)) |
| 305 | printf("\n"); |
| 306 | }; |
| 307 | printf("\n"); |
| 308 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 311 | char *snprint_packet(struct gsn_t *gsn, struct sockaddr_in *peer, |
| 312 | void *pack, unsigned len, char *buf, int size) |
| 313 | { |
| 314 | unsigned int n; |
| 315 | int pos; |
| 316 | snprintf(buf, size, "Packet from %s:%u, length: %d, content:", |
| 317 | inet_ntoa(peer->sin_addr), ntohs(peer->sin_port), len); |
| 318 | buf[size - 1] = 0; |
| 319 | pos = strlen(buf); |
| 320 | for (n = 0; n < len; n++) { |
| 321 | if ((pos + 4) < size) { |
| 322 | sprintf((buf + pos), " %02hhx", |
| 323 | ((unsigned char *)pack)[n]); |
| 324 | pos += 3; |
| 325 | } |
| 326 | } |
| 327 | buf[pos] = 0; |
| 328 | return buf; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 329 | } |
| 330 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 331 | /* *********************************************************** |
| 332 | * Reliable delivery of signalling messages |
| 333 | * |
| 334 | * Sequence numbers are used for both signalling messages and |
| 335 | * data messages. |
| 336 | * |
| 337 | * For data messages each tunnel maintains a sequence counter, |
| 338 | * which is incremented by one each time a new data message |
| 339 | * is sent. The sequence number starts at (0) zero at tunnel |
| 340 | * establishment, and wraps around at 65535 (29.060 9.3.1.1 |
| 341 | * and 09.60 8.1.1.1). The sequence numbers are either ignored, |
| 342 | * or can be used to check the validity of the message in the |
| 343 | * receiver, or for reordering af packets. |
| 344 | * |
| 345 | * For signalling messages the sequence number is used by |
| 346 | * signalling messages for which a response is defined. A response |
| 347 | * message should copy the sequence from the corresponding request |
| 348 | * message. The sequence number "unambiguously" identifies a request |
| 349 | * message within a given path, with a path being defined as a set of |
| 350 | * two endpoints (29.060 8.2, 29.060 7.6, 09.60 7.8). "All request |
| 351 | * messages shall be responded to, and all response messages associated |
| 352 | * with a certain request shall always include the same information" |
| 353 | * |
| 354 | * We take this to mean that the GSN transmitting a request is free to |
| 355 | * choose the sequence number, as long as it is unique within a given path. |
| 356 | * It means that we are allowed to count backwards, or roll over at 17 |
| 357 | * if we prefer that. It also means that we can use the same counter for |
| 358 | * all paths. This has the advantage that the transmitted request sequence |
| 359 | * numbers are unique within each GSN, and also we dont have to mess around |
| 360 | * with path setup and teardown. |
| 361 | * |
| 362 | * If a response message is lost, the request will be retransmitted, and |
| 363 | * the receiving GSN will receive a "duplicated" request. The standard |
| 364 | * requires the receiving GSN to send a response, with the same information |
| 365 | * as in the original response. For most messages this happens automatically: |
| 366 | * |
| 367 | * Echo: Automatically dublicates the original response |
| 368 | * Create pdp context: The SGSN may send create context request even if |
| 369 | * a context allready exist (imsi+nsapi?). This means that the reply will |
| 370 | automatically dublicate the original response. It might however have |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 371 | * side effects in the application which is asked twice to validate |
| 372 | * the login. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 373 | * Update pdp context: Automatically dublicates the original response??? |
| 374 | * Delete pdp context. Automatically in gtp0, but in gtp1 will generate |
| 375 | * a nonexist reply message. |
| 376 | * |
| 377 | * The correct solution will be to make a queue containing response messages. |
| 378 | * This queue should be checked whenever a request is received. If the |
| 379 | * response is allready in the queue that response should be transmitted. |
| 380 | * It should be possible to find messages in this queue on the basis of |
| 381 | * the sequence number and peer GSN IP address (The sequense number is unique |
| 382 | * within each path). This need to be implemented by a hash table. Furthermore |
| 383 | * it should be possibly to delete messages based on a timeout. This can be |
| 384 | * achieved by means of a linked list. The timeout value need to be larger |
| 385 | * than T3-RESPONSE * N3-REQUESTS (recommended value 5). These timers are |
| 386 | * set in the peer GSN, so there is no way to know these parameters. On the |
| 387 | * other hand the timeout value need to be so small that we do not receive |
| 388 | * wraparound sequence numbere before the message is deleted. 60 seconds is |
| 389 | * probably not a bad choise. |
| 390 | * |
| 391 | * This queue however is first really needed from gtp1. |
| 392 | * |
| 393 | * gtp_req: |
| 394 | * Send off a signalling message with appropiate sequence |
| 395 | * number. Store packet in queue. |
| 396 | * gtp_conf: |
| 397 | * Remove an incoming confirmation from the queue |
| 398 | * gtp_resp: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 399 | * Send off a response to a request. Use the same sequence |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 400 | * number in the response as in the request. |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 401 | * gtp_notification: |
| 402 | * Send off a notification message. This is neither a request nor |
| 403 | * a response. Both TEI and SEQ are zero. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 404 | * gtp_retrans: |
| 405 | * Retransmit any outstanding packets which have exceeded |
| 406 | * a predefined timeout. |
| 407 | *************************************************************/ |
| 408 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 409 | int gtp_req(struct gsn_t *gsn, int version, struct pdp_t *pdp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 410 | union gtp_packet *packet, int len, |
| 411 | struct in_addr *inetaddr, void *cbp) |
| 412 | { |
| 413 | struct sockaddr_in addr; |
| 414 | struct qmsg_t *qmsg; |
| 415 | int fd; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 416 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 417 | memset(&addr, 0, sizeof(addr)); |
| 418 | addr.sin_family = AF_INET; |
| 419 | addr.sin_addr = *inetaddr; |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 420 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 421 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 422 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 423 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 424 | if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */ |
| 425 | addr.sin_port = htons(GTP0_PORT); |
| 426 | packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE); |
| 427 | packet->gtp0.h.seq = hton16(gsn->seq_next); |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 428 | if (pdp) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 429 | packet->gtp0.h.tid = |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame^] | 430 | htobe64(pdp_gettid(pdp->imsi, pdp->nsapi)); |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 431 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 432 | if (pdp && ((packet->gtp0.h.type == GTP_GPDU) |
| 433 | || (packet->gtp0.h.type == GTP_ERROR))) |
| 434 | packet->gtp0.h.flow = hton16(pdp->flru); |
| 435 | else if (pdp) |
| 436 | packet->gtp0.h.flow = hton16(pdp->flrc); |
| 437 | fd = gsn->fd0; |
| 438 | } else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */ |
| 439 | addr.sin_port = htons(GTP1C_PORT); |
| 440 | packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT); |
| 441 | packet->gtp1l.h.seq = hton16(gsn->seq_next); |
| 442 | if (pdp && ((packet->gtp1l.h.type == GTP_GPDU) || |
| 443 | (packet->gtp1l.h.type == GTP_ERROR))) |
| 444 | packet->gtp1l.h.tei = hton32(pdp->teid_gn); |
| 445 | else if (pdp) |
| 446 | packet->gtp1l.h.tei = hton32(pdp->teic_gn); |
| 447 | fd = gsn->fd1c; |
| 448 | } else { |
| 449 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag"); |
| 450 | return -1; |
| 451 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 452 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 453 | if (sendto(fd, packet, len, 0, |
| 454 | (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 455 | gsn->err_sendto++; |
| 456 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 457 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, |
| 458 | (unsigned long)&packet, len, strerror(errno)); |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | /* Use new queue structure */ |
| 463 | if (queue_newmsg(gsn->queue_req, &qmsg, &addr, gsn->seq_next)) { |
| 464 | gsn->err_queuefull++; |
| 465 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 466 | "Retransmit queue is full"); |
| 467 | } else { |
| 468 | memcpy(&qmsg->p, packet, sizeof(union gtp_packet)); |
| 469 | qmsg->l = len; |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 470 | qmsg->timeout = time(NULL) + T3_REQUEST; /* When to timeout */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 471 | qmsg->retrans = 0; /* No retransmissions so far */ |
| 472 | qmsg->cbp = cbp; |
| 473 | qmsg->type = ntoh8(packet->gtp0.h.type); |
| 474 | qmsg->fd = fd; |
| 475 | } |
| 476 | gsn->seq_next++; /* Count up this time */ |
| 477 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* gtp_conf |
| 481 | * Remove signalling packet from retransmission queue. |
| 482 | * return 0 on success, EOF if packet was not found */ |
| 483 | |
| 484 | int gtp_conf(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 485 | union gtp_packet *packet, int len, uint8_t * type, void **cbp) |
| 486 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 487 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 488 | uint16_t seq; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 489 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 490 | if ((packet->gtp0.h.flags & 0xe0) == 0x00) |
| 491 | seq = ntoh16(packet->gtp0.h.seq); |
| 492 | else if ((packet->gtp1l.h.flags & 0xe2) == 0x22) |
| 493 | seq = ntoh16(packet->gtp1l.h.seq); |
| 494 | else { |
| 495 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, packet, len, |
| 496 | "Unknown GTP packet version"); |
| 497 | return EOF; |
| 498 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 499 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 500 | if (queue_freemsg_seq(gsn->queue_req, peer, seq, type, cbp)) { |
| 501 | gsn->err_seq++; |
| 502 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, packet, len, |
| 503 | "Confirmation packet not found in queue"); |
| 504 | return EOF; |
| 505 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 506 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 507 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 510 | int gtp_retrans(struct gsn_t *gsn) |
| 511 | { |
| 512 | /* Retransmit any outstanding packets */ |
| 513 | /* Remove from queue if maxretrans exceeded */ |
| 514 | time_t now; |
| 515 | struct qmsg_t *qmsg; |
| 516 | now = time(NULL); |
| 517 | /*printf("Retrans: New beginning %d\n", (int) now); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 518 | |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 519 | /* get first element in queue, as long as the timeout of that |
| 520 | * element has expired */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 521 | while ((!queue_getfirst(gsn->queue_req, &qmsg)) && |
| 522 | (qmsg->timeout <= now)) { |
| 523 | /*printf("Retrans timeout found: %d\n", (int) time(NULL)); */ |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 524 | if (qmsg->retrans > N3_REQUESTS) { /* To many retrans */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 525 | if (gsn->cb_conf) |
| 526 | gsn->cb_conf(qmsg->type, EOF, NULL, qmsg->cbp); |
| 527 | queue_freemsg(gsn->queue_req, qmsg); |
| 528 | } else { |
| 529 | if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0, |
| 530 | (struct sockaddr *)&qmsg->peer, |
| 531 | sizeof(struct sockaddr_in)) < 0) { |
| 532 | gsn->err_sendto++; |
| 533 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 534 | "Sendto(fd0=%d, msg=%lx, len=%d) failed: Error = %s", |
| 535 | gsn->fd0, (unsigned long)&qmsg->p, |
| 536 | qmsg->l, strerror(errno)); |
| 537 | } |
| 538 | queue_back(gsn->queue_req, qmsg); |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 539 | qmsg->timeout = now + T3_REQUEST; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 540 | qmsg->retrans++; |
| 541 | } |
| 542 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 543 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 544 | /* Also clean up reply timeouts */ |
| 545 | while ((!queue_getfirst(gsn->queue_resp, &qmsg)) && |
| 546 | (qmsg->timeout < now)) { |
| 547 | /*printf("Retrans (reply) timeout found: %d\n", (int) time(NULL)); */ |
| 548 | queue_freemsg(gsn->queue_resp, qmsg); |
| 549 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 550 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 551 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 554 | int gtp_retranstimeout(struct gsn_t *gsn, struct timeval *timeout) |
| 555 | { |
| 556 | time_t now, later; |
| 557 | struct qmsg_t *qmsg; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 558 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 559 | if (queue_getfirst(gsn->queue_req, &qmsg)) { |
| 560 | timeout->tv_sec = 10; |
| 561 | timeout->tv_usec = 0; |
| 562 | } else { |
| 563 | now = time(NULL); |
| 564 | later = qmsg->timeout; |
| 565 | timeout->tv_sec = later - now; |
| 566 | timeout->tv_usec = 0; |
| 567 | if (timeout->tv_sec < 0) |
| 568 | timeout->tv_sec = 0; /* No negative allowed */ |
| 569 | if (timeout->tv_sec > 10) |
| 570 | timeout->tv_sec = 10; /* Max sleep for 10 sec */ |
| 571 | } |
| 572 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 575 | int gtp_resp(int version, struct gsn_t *gsn, struct pdp_t *pdp, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 576 | union gtp_packet *packet, int len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 577 | struct sockaddr_in *peer, int fd, uint16_t seq, uint64_t tid) |
| 578 | { |
| 579 | struct qmsg_t *qmsg; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 580 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 581 | if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */ |
| 582 | packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE); |
| 583 | packet->gtp0.h.seq = hton16(seq); |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 584 | packet->gtp0.h.tid = htobe64(tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 585 | if (pdp && ((packet->gtp0.h.type == GTP_GPDU) || |
| 586 | (packet->gtp0.h.type == GTP_ERROR))) |
| 587 | packet->gtp0.h.flow = hton16(pdp->flru); |
| 588 | else if (pdp) |
| 589 | packet->gtp0.h.flow = hton16(pdp->flrc); |
| 590 | } else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */ |
| 591 | packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT); |
| 592 | packet->gtp1l.h.seq = hton16(seq); |
| 593 | if (pdp && (fd == gsn->fd1u)) |
| 594 | packet->gtp1l.h.tei = hton32(pdp->teid_gn); |
| 595 | else if (pdp) |
| 596 | packet->gtp1l.h.tei = hton32(pdp->teic_gn); |
| 597 | } else { |
| 598 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag"); |
| 599 | return -1; |
| 600 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 601 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 602 | if (fcntl(fd, F_SETFL, 0)) { |
| 603 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 604 | return -1; |
| 605 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 606 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 607 | if (sendto(fd, packet, len, 0, |
| 608 | (struct sockaddr *)peer, sizeof(struct sockaddr_in)) < 0) { |
| 609 | gsn->err_sendto++; |
| 610 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 611 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, |
| 612 | (unsigned long)&packet, len, strerror(errno)); |
| 613 | return -1; |
| 614 | } |
| 615 | |
| 616 | /* Use new queue structure */ |
| 617 | if (queue_newmsg(gsn->queue_resp, &qmsg, peer, seq)) { |
| 618 | gsn->err_queuefull++; |
| 619 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 620 | "Retransmit queue is full"); |
| 621 | } else { |
| 622 | memcpy(&qmsg->p, packet, sizeof(union gtp_packet)); |
| 623 | qmsg->l = len; |
| 624 | qmsg->timeout = time(NULL) + 60; /* When to timeout */ |
| 625 | qmsg->retrans = 0; /* No retransmissions so far */ |
| 626 | qmsg->cbp = NULL; |
| 627 | qmsg->type = 0; |
| 628 | qmsg->fd = fd; |
| 629 | } |
| 630 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 631 | } |
| 632 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 633 | int gtp_notification(struct gsn_t *gsn, int version, |
| 634 | union gtp_packet *packet, int len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 635 | struct sockaddr_in *peer, int fd, uint16_t seq) |
| 636 | { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 637 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 638 | struct sockaddr_in addr; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 639 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 640 | memcpy(&addr, peer, sizeof(addr)); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 641 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 642 | /* In GTP0 notifications are treated as replies. In GTP1 they |
| 643 | are requests for which there is no reply */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 644 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 645 | if (fd == gsn->fd1c) |
| 646 | addr.sin_port = htons(GTP1C_PORT); |
| 647 | else if (fd == gsn->fd1u) |
| 648 | addr.sin_port = htons(GTP1C_PORT); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 649 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 650 | if ((packet->flags & 0xe0) == 0x00) { /* Version 0 */ |
| 651 | packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE); |
| 652 | packet->gtp0.h.seq = hton16(seq); |
| 653 | } else if ((packet->flags & 0xe2) == 0x22) { /* Version 1 with seq */ |
| 654 | packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT); |
| 655 | packet->gtp1l.h.seq = hton16(seq); |
| 656 | } else { |
| 657 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown packet flag"); |
| 658 | return -1; |
| 659 | } |
| 660 | |
| 661 | if (fcntl(fd, F_SETFL, 0)) { |
| 662 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 663 | return -1; |
| 664 | } |
| 665 | |
| 666 | if (sendto(fd, packet, len, 0, |
| 667 | (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { |
| 668 | gsn->err_sendto++; |
| 669 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 670 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, |
| 671 | (unsigned long)&packet, len, strerror(errno)); |
| 672 | return -1; |
| 673 | } |
| 674 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 677 | int gtp_dublicate(struct gsn_t *gsn, int version, |
| 678 | struct sockaddr_in *peer, uint16_t seq) |
| 679 | { |
| 680 | struct qmsg_t *qmsg; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 681 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 682 | if (queue_seqget(gsn->queue_resp, &qmsg, peer, seq)) { |
| 683 | return EOF; /* Notfound */ |
| 684 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 685 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 686 | if (fcntl(qmsg->fd, F_SETFL, 0)) { |
| 687 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 688 | return -1; |
| 689 | } |
| 690 | |
| 691 | if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0, |
| 692 | (struct sockaddr *)peer, sizeof(struct sockaddr_in)) < 0) { |
| 693 | gsn->err_sendto++; |
| 694 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 695 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", |
| 696 | qmsg->fd, (unsigned long)&qmsg->p, qmsg->l, |
| 697 | strerror(errno)); |
| 698 | } |
| 699 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 700 | } |
| 701 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 702 | /* Perform restoration and recovery error handling as described in 29.060 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 703 | static void log_restart(struct gsn_t *gsn) |
| 704 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 705 | FILE *f; |
Emmanuel Bretelle | 111e054 | 2010-09-06 19:49:16 +0200 | [diff] [blame] | 706 | int i, rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 707 | int counter = 0; |
| 708 | char filename[NAMESIZE]; |
| 709 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 710 | filename[NAMESIZE - 1] = 0; /* No null term. guarantee by strncpy */ |
| 711 | strncpy(filename, gsn->statedir, NAMESIZE - 1); |
| 712 | strncat(filename, RESTART_FILE, NAMESIZE - 1 - sizeof(RESTART_FILE)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 713 | |
| 714 | i = umask(022); |
| 715 | |
| 716 | /* We try to open file. On failure we will later try to create file */ |
| 717 | if (!(f = fopen(filename, "r"))) { |
jjako | 581c9f0 | 2003-10-22 11:28:20 +0000 | [diff] [blame] | 718 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 719 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 720 | "State information file (%s) not found. Creating new file.", |
| 721 | filename); |
| 722 | } else { |
| 723 | umask(i); |
| 724 | rc = fscanf(f, "%d", &counter); |
| 725 | if (rc != 1) { |
| 726 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 727 | "fscanf failed to read counter value"); |
| 728 | return; |
| 729 | } |
| 730 | if (fclose(f)) { |
| 731 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 732 | "fclose failed: Error = %s", strerror(errno)); |
| 733 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 734 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 735 | |
| 736 | gsn->restart_counter = (unsigned char)counter; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 737 | gsn->restart_counter++; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 738 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 739 | if (!(f = fopen(filename, "w"))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 740 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 741 | "fopen(path=%s, mode=%s) failed: Error = %s", filename, |
| 742 | "w", strerror(errno)); |
| 743 | return; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | umask(i); |
| 747 | fprintf(f, "%d\n", gsn->restart_counter); |
| 748 | if (fclose(f)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 749 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 750 | "fclose failed: Error = %s", strerror(errno)); |
| 751 | return; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 755 | int gtp_new(struct gsn_t **gsn, char *statedir, struct in_addr *listen, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 756 | int mode) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 757 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 758 | struct sockaddr_in addr; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 759 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 760 | syslog(LOG_ERR, "GTP: gtp_newgsn() started"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 761 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 762 | *gsn = calloc(sizeof(struct gsn_t), 1); /* TODO */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 763 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 764 | (*gsn)->statedir = statedir; |
| 765 | log_restart(*gsn); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 766 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 767 | /* Initialise sequence number */ |
| 768 | (*gsn)->seq_next = (*gsn)->restart_counter * 1024; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 769 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 770 | /* Initialise request retransmit queue */ |
| 771 | queue_new(&(*gsn)->queue_req); |
| 772 | queue_new(&(*gsn)->queue_resp); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 773 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 774 | /* Initialise pdp table */ |
| 775 | pdp_init(); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 776 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 777 | /* Initialise call back functions */ |
| 778 | (*gsn)->cb_create_context_ind = 0; |
| 779 | (*gsn)->cb_delete_context = 0; |
| 780 | (*gsn)->cb_unsup_ind = 0; |
| 781 | (*gsn)->cb_conf = 0; |
| 782 | (*gsn)->cb_data_ind = 0; |
| 783 | |
| 784 | /* Store function parameters */ |
| 785 | (*gsn)->gsnc = *listen; |
| 786 | (*gsn)->gsnu = *listen; |
| 787 | (*gsn)->mode = mode; |
| 788 | |
| 789 | /* Create GTP version 0 socket */ |
| 790 | if (((*gsn)->fd0 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 791 | (*gsn)->err_socket++; |
| 792 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 793 | "socket(domain=%d, type=%d, protocol=%d) failed: Error = %s", |
| 794 | 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(GTP0_PORT); |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 802 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 803 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 804 | #endif |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 805 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 806 | if (bind((*gsn)->fd0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 807 | (*gsn)->err_socket++; |
| 808 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 809 | "bind(fd0=%d, addr=%lx, len=%d) failed: Error = %s", |
| 810 | (*gsn)->fd0, (unsigned long)&addr, sizeof(addr), |
| 811 | strerror(errno)); |
| 812 | return -1; |
| 813 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 814 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 815 | /* Create GTP version 1 control plane socket */ |
| 816 | if (((*gsn)->fd1c = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 817 | (*gsn)->err_socket++; |
| 818 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 819 | "socket(domain=%d, type=%d, protocol=%d) failed: Error = %s", |
| 820 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
| 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | memset(&addr, 0, sizeof(addr)); |
| 825 | addr.sin_family = AF_INET; |
| 826 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 827 | addr.sin_port = htons(GTP1C_PORT); |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 828 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 829 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 830 | #endif |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 831 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 832 | if (bind((*gsn)->fd1c, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 833 | (*gsn)->err_socket++; |
| 834 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 835 | "bind(fd1c=%d, addr=%lx, len=%d) failed: Error = %s", |
| 836 | (*gsn)->fd1c, (unsigned long)&addr, sizeof(addr), |
| 837 | strerror(errno)); |
| 838 | return -1; |
| 839 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 840 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 841 | /* Create GTP version 1 user plane socket */ |
| 842 | if (((*gsn)->fd1u = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 843 | (*gsn)->err_socket++; |
| 844 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 845 | "socket(domain=%d, type=%d, protocol=%d) failed: Error = %s", |
| 846 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
| 847 | return -1; |
| 848 | } |
| 849 | |
| 850 | memset(&addr, 0, sizeof(addr)); |
| 851 | addr.sin_family = AF_INET; |
| 852 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 853 | addr.sin_port = htons(GTP1U_PORT); |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 854 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 855 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 856 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 857 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 858 | if (bind((*gsn)->fd1u, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 859 | (*gsn)->err_socket++; |
| 860 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 861 | "bind(fd1c=%d, addr=%lx, len=%d) failed: Error = %s", |
| 862 | (*gsn)->fd1c, (unsigned long)&addr, sizeof(addr), |
| 863 | strerror(errno)); |
| 864 | return -1; |
| 865 | } |
| 866 | |
| 867 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 870 | int gtp_free(struct gsn_t *gsn) |
| 871 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 872 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 873 | /* Clean up retransmit queues */ |
| 874 | queue_free(gsn->queue_req); |
| 875 | queue_free(gsn->queue_resp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 876 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 877 | close(gsn->fd0); |
| 878 | close(gsn->fd1c); |
| 879 | close(gsn->fd1u); |
| 880 | |
| 881 | free(gsn); |
| 882 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* *********************************************************** |
| 886 | * Path management messages |
| 887 | * Messages: echo and version not supported. |
| 888 | * A path is connection between two UDP/IP endpoints |
| 889 | * |
| 890 | * A path is either using GTP0 or GTP1. A path can be |
| 891 | * established by any kind of GTP message?? |
| 892 | |
| 893 | * Which source port to use? |
| 894 | * GTP-C request destination port is 2123/3386 |
| 895 | * GTP-U request destination port is 2152/3386 |
| 896 | * T-PDU destination port is 2152/3386. |
| 897 | * For the above messages the source port is locally allocated. |
| 898 | * For response messages src=rx-dst and dst=rx-src. |
| 899 | * For simplicity we should probably use 2123+2152/3386 as |
| 900 | * src port even for the cases where src can be locally |
| 901 | * allocated. This also means that we have to listen only to |
| 902 | * the same ports. |
| 903 | * For response messages we need to be able to respond to |
| 904 | * the relevant src port even if it is locally allocated by |
| 905 | * the peer. |
| 906 | * |
| 907 | * The need for path management! |
| 908 | * We might need to keep a list of active paths. This might |
| 909 | * be in the form of remote IP address + UDP port numbers. |
| 910 | * (We will consider a path astablished if we have a context |
| 911 | * with the node in question) |
| 912 | *************************************************************/ |
| 913 | |
| 914 | /* Send off an echo request */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 915 | int gtp_echo_req(struct gsn_t *gsn, int version, void *cbp, |
| 916 | struct in_addr *inetaddr) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 917 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 918 | union gtp_packet packet; |
| 919 | unsigned int length = get_default_gtp(version, GTP_ECHO_REQ, &packet); |
| 920 | return gtp_req(gsn, version, NULL, &packet, length, inetaddr, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 921 | } |
| 922 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 923 | /* Send off an echo reply */ |
| 924 | int gtp_echo_resp(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 925 | struct sockaddr_in *peer, int fd, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 926 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 927 | union gtp_packet packet; |
| 928 | unsigned int length = get_default_gtp(version, GTP_ECHO_RSP, &packet); |
| 929 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 930 | gsn->restart_counter); |
| 931 | return gtp_resp(version, gsn, NULL, &packet, length, peer, fd, |
| 932 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 933 | } |
| 934 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 935 | /* Handle a received echo request */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 936 | int gtp_echo_ind(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 937 | int fd, void *pack, unsigned len) |
| 938 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 939 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 940 | /* Check if it was a dublicate request */ |
| 941 | if (!gtp_dublicate(gsn, 0, peer, get_seq(pack))) |
| 942 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 943 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 944 | /* Send off reply to request */ |
| 945 | return gtp_echo_resp(gsn, version, peer, fd, pack, len); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /* Handle a received echo reply */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 949 | int gtp_echo_conf(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 950 | void *pack, unsigned len) |
| 951 | { |
| 952 | union gtpie_member *ie[GTPIE_SIZE]; |
| 953 | unsigned char recovery; |
| 954 | void *cbp = NULL; |
| 955 | uint8_t type = 0; |
| 956 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 957 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 958 | /* Remove packet from queue */ |
| 959 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) |
| 960 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 961 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 962 | /* Extract information elements into a pointer array */ |
| 963 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 964 | gsn->invalid++; |
| 965 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 966 | "Invalid message format"); |
| 967 | if (gsn->cb_conf) |
| 968 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 969 | return EOF; |
| 970 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 971 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 972 | if (gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 973 | gsn->missing++; |
| 974 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 975 | "Missing mandatory field"); |
| 976 | if (gsn->cb_conf) |
| 977 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 978 | return EOF; |
| 979 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 980 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 981 | /* Echo reply packages does not have a cause information element */ |
| 982 | /* Instead we return the recovery number in the callback function */ |
| 983 | if (gsn->cb_conf) |
| 984 | gsn->cb_conf(type, recovery, NULL, cbp); |
Harald Welte | 629e986 | 2010-12-24 20:58:09 +0100 | [diff] [blame] | 985 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 986 | if (gsn->cb_recovery) |
| 987 | gsn->cb_recovery(peer, recovery); |
| 988 | |
| 989 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* Send off a Version Not Supported message */ |
| 993 | /* This message is somewhat special in that it actually is a |
| 994 | * response to some other message with unsupported GTP version |
| 995 | * For this reason it has parameters like a response, and does |
| 996 | * its own message transmission. No signalling queue is used |
| 997 | * The reply is sent to the peer IP and peer UDP. This means that |
| 998 | * the peer will be receiving a GTP0 message on a GTP1 port! |
| 999 | * In practice however this will never happen as a GTP0 GSN will |
| 1000 | * only listen to the GTP0 port, and therefore will never receive |
| 1001 | * anything else than GTP0 */ |
| 1002 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1003 | int gtp_unsup_req(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 1004 | int fd, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1005 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1006 | union gtp_packet packet; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1007 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1008 | /* GTP 1 is the highest supported protocol */ |
| 1009 | unsigned int length = get_default_gtp(1, GTP_NOT_SUPPORTED, &packet); |
| 1010 | return gtp_notification(gsn, version, &packet, length, peer, fd, 0); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | /* Handle a Version Not Supported message */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1014 | int gtp_unsup_ind(struct gsn_t *gsn, struct sockaddr_in *peer, |
| 1015 | void *pack, unsigned len) |
| 1016 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1017 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1018 | if (gsn->cb_unsup_ind) |
| 1019 | gsn->cb_unsup_ind(peer); |
| 1020 | |
| 1021 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1024 | /* Send off an Supported Extension Headers Notification */ |
| 1025 | int gtp_extheader_req(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 1026 | int fd, void *pack, unsigned len) |
| 1027 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1028 | union gtp_packet packet; |
| 1029 | unsigned int length = |
| 1030 | get_default_gtp(version, GTP_SUPP_EXT_HEADER, &packet); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1031 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1032 | uint8_t pdcp_pdu = GTP_EXT_PDCP_PDU; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1033 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1034 | if (version < 1) |
| 1035 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1036 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1037 | /* We report back that we support only PDCP PDU headers */ |
| 1038 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EXT_HEADER_T, |
| 1039 | sizeof(pdcp_pdu), &pdcp_pdu); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1040 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1041 | return gtp_notification(gsn, version, &packet, length, |
| 1042 | peer, fd, get_seq(pack)); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* Handle a Supported Extension Headers Notification */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1046 | int gtp_extheader_ind(struct gsn_t *gsn, struct sockaddr_in *peer, |
| 1047 | void *pack, unsigned len) |
| 1048 | { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1049 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1050 | if (gsn->cb_extheader_ind) |
| 1051 | gsn->cb_extheader_ind(peer); |
| 1052 | |
| 1053 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1056 | /* *********************************************************** |
| 1057 | * Session management messages |
| 1058 | * Messages: create, update and delete PDP context |
| 1059 | * |
| 1060 | * Information storage |
| 1061 | * Information storage for each PDP context is defined in |
| 1062 | * 23.060 section 13.3. Includes IMSI, MSISDN, APN, PDP-type, |
| 1063 | * PDP-address (IP address), sequence numbers, charging ID. |
| 1064 | * For the SGSN it also includes radio related mobility |
| 1065 | * information. |
| 1066 | *************************************************************/ |
| 1067 | |
Harald Welte | 7b3347b | 2010-05-15 12:18:46 +0200 | [diff] [blame] | 1068 | /* API: Send Create PDP Context Request (7.3.1) */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1069 | extern int gtp_create_context_req(struct gsn_t *gsn, struct pdp_t *pdp, |
| 1070 | void *cbp) |
| 1071 | { |
| 1072 | union gtp_packet packet; |
| 1073 | unsigned int length = |
| 1074 | get_default_gtp(pdp->version, GTP_CREATE_PDP_REQ, &packet); |
| 1075 | struct pdp_t *linked_pdp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1076 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1077 | /* TODO: Secondary PDP Context Activation Procedure */ |
| 1078 | /* In secondary activation procedure the PDP context is identified |
| 1079 | by tei in the header. The following fields are omitted: Selection |
| 1080 | mode, IMSI, MSISDN, End User Address, Access Point Name and |
| 1081 | Protocol Configuration Options */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1082 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1083 | if (pdp->secondary) { |
| 1084 | if (pdp_getgtp1(&linked_pdp, pdp->teic_own)) { |
| 1085 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 1086 | "Unknown linked PDP context"); |
| 1087 | return EOF; |
| 1088 | } |
| 1089 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1090 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1091 | if (pdp->version == 0) { |
| 1092 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1093 | sizeof(pdp->qos_req0), pdp->qos_req0); |
| 1094 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1095 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1096 | /* Section 7.7.2 */ |
| 1097 | if (pdp->version == 1) { |
| 1098 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1099 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI, |
| 1100 | sizeof(pdp->imsi), (uint8_t *) & pdp->imsi); |
| 1101 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1102 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1103 | /* Section 7.7.3 Routing Area Information */ |
| 1104 | if (pdp->rai_given == 1) |
| 1105 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_RAI, |
| 1106 | pdp->rai.l, (uint8_t *) & pdp->rai.v); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 1107 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1108 | /* Section 7.7.11 */ |
| 1109 | if (pdp->norecovery_given == 0) |
| 1110 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1111 | gsn->restart_counter); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 1112 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1113 | /* Section 7.7.12 */ |
| 1114 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1115 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_SELECTION_MODE, |
| 1116 | pdp->selmode); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1117 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1118 | if (pdp->version == 0) { |
| 1119 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, pdp->fllu); |
| 1120 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, pdp->fllc); |
| 1121 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1122 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1123 | /* Section 7.7.13 */ |
| 1124 | if (pdp->version == 1) { |
| 1125 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1126 | pdp->teid_own); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1127 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1128 | /* Section 7.7.14 */ |
| 1129 | if (!pdp->teic_confirmed) |
| 1130 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1131 | pdp->teic_own); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1132 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1133 | /* Section 7.7.17 */ |
| 1134 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, pdp->nsapi); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1135 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1136 | /* Section 7.7.17 */ |
| 1137 | if (pdp->secondary) /* Secondary PDP Context Activation Procedure */ |
| 1138 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, |
| 1139 | linked_pdp->nsapi); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1140 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1141 | /* Section 7.7.23 */ |
| 1142 | if (pdp->cch_pdp) /* Only include charging if flags are set */ |
| 1143 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_CHARGING_C, |
| 1144 | pdp->cch_pdp); |
| 1145 | } |
jjako | 9b4971d | 2004-05-27 20:30:19 +0000 | [diff] [blame] | 1146 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1147 | /* TODO |
| 1148 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_REF, |
| 1149 | pdp->traceref); |
| 1150 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_TYPE, |
| 1151 | pdp->tracetype); */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1152 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1153 | /* Section 7.7.27 */ |
| 1154 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1155 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1156 | pdp->eua.l, pdp->eua.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1157 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1158 | /* Section 7.7.30 */ |
| 1159 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1160 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_APN, |
| 1161 | pdp->apn_use.l, pdp->apn_use.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1162 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1163 | /* Section 7.7.31 */ |
| 1164 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1165 | if (pdp->pco_req.l) |
| 1166 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO, |
| 1167 | pdp->pco_req.l, pdp->pco_req.v); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1168 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1169 | /* Section 7.7.32 */ |
| 1170 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1171 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 1172 | /* Section 7.7.32 */ |
| 1173 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1174 | pdp->gsnlu.l, pdp->gsnlu.v); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1175 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1176 | /* Section 7.7.33 */ |
| 1177 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1178 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_MSISDN, |
| 1179 | pdp->msisdn.l, pdp->msisdn.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1180 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1181 | /* Section 7.7.34 */ |
| 1182 | if (pdp->version == 1) |
| 1183 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1184 | pdp->qos_req.l, pdp->qos_req.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1185 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1186 | /* Section 7.7.36 */ |
| 1187 | if ((pdp->version == 1) && pdp->tft.l) |
| 1188 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TFT, |
| 1189 | pdp->tft.l, pdp->tft.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1190 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1191 | /* Section 7.7.41 */ |
| 1192 | if ((pdp->version == 1) && pdp->triggerid.l) |
| 1193 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TRIGGER_ID, |
| 1194 | pdp->triggerid.l, pdp->triggerid.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1195 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1196 | /* Section 7.7.42 */ |
| 1197 | if ((pdp->version == 1) && pdp->omcid.l) |
| 1198 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_OMC_ID, |
| 1199 | pdp->omcid.l, pdp->omcid.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1200 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1201 | /* new R7 fields */ |
| 1202 | if (pdp->rattype_given == 1) |
| 1203 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_RAT_TYPE, |
| 1204 | pdp->rattype.l, pdp->rattype.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1205 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1206 | if (pdp->userloc_given == 1) |
| 1207 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_USER_LOC, |
| 1208 | pdp->userloc.l, pdp->userloc.v); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1209 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1210 | if (pdp->mstz_given == 1) |
| 1211 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_MS_TZ, |
| 1212 | pdp->mstz.l, pdp->mstz.v); |
| 1213 | |
| 1214 | if (pdp->imeisv_given == 1) |
| 1215 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_IMEI_SV, |
| 1216 | pdp->imeisv.l, pdp->imeisv.v); |
| 1217 | |
| 1218 | /* TODO hisaddr0 */ |
| 1219 | gtp_req(gsn, pdp->version, pdp, &packet, length, &pdp->hisaddr0, cbp); |
| 1220 | |
| 1221 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1224 | /* API: Application response to context indication */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1225 | int gtp_create_context_resp(struct gsn_t *gsn, struct pdp_t *pdp, int cause) |
| 1226 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1227 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1228 | /* Now send off a reply to the peer */ |
| 1229 | gtp_create_pdp_resp(gsn, pdp->version, pdp, cause); |
| 1230 | |
| 1231 | if (cause != GTPCAUSE_ACC_REQ) { |
| 1232 | pdp_freepdp(pdp); |
| 1233 | } |
| 1234 | |
| 1235 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /* API: Register create context indication callback */ |
| 1239 | int gtp_set_cb_create_context_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1240 | int (*cb_create_context_ind) (struct pdp_t * |
| 1241 | pdp)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1242 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1243 | gsn->cb_create_context_ind = cb_create_context_ind; |
| 1244 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1247 | /* Send Create PDP Context Response */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1248 | int gtp_create_pdp_resp(struct gsn_t *gsn, int version, struct pdp_t *pdp, |
| 1249 | uint8_t cause) |
| 1250 | { |
| 1251 | union gtp_packet packet; |
| 1252 | unsigned int length = |
| 1253 | get_default_gtp(version, GTP_CREATE_PDP_RSP, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1254 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1255 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1256 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1257 | if (cause == GTPCAUSE_ACC_REQ) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1258 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1259 | if (version == 0) |
| 1260 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1261 | sizeof(pdp->qos_neg0), pdp->qos_neg0); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1262 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1263 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_REORDER, |
| 1264 | pdp->reorder); |
| 1265 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1266 | gsn->restart_counter); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1267 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1268 | if (version == 0) { |
| 1269 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, |
| 1270 | pdp->fllu); |
| 1271 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, |
| 1272 | pdp->fllc); |
| 1273 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1274 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1275 | if (version == 1) { |
| 1276 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1277 | pdp->teid_own); |
| 1278 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1279 | pdp->teic_own); |
| 1280 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1281 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1282 | /* TODO: We use teic_own as charging ID */ |
| 1283 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID, |
| 1284 | pdp->teic_own); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1285 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1286 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1287 | pdp->eua.l, pdp->eua.v); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1288 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1289 | if (pdp->pco_neg.l) { /* Optional PCO */ |
| 1290 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO, |
| 1291 | pdp->pco_neg.l, pdp->pco_neg.v); |
| 1292 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1293 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1294 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1295 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 1296 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1297 | pdp->gsnlu.l, pdp->gsnlu.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1298 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1299 | if (version == 1) |
| 1300 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1301 | pdp->qos_neg.l, pdp->qos_neg.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1302 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1303 | /* TODO: Charging gateway address */ |
| 1304 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1305 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1306 | return gtp_resp(version, gsn, pdp, &packet, length, &pdp->sa_peer, |
| 1307 | pdp->fd, pdp->seq, pdp->tid); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | /* Handle Create PDP Context Request */ |
| 1311 | int gtp_create_pdp_ind(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1312 | struct sockaddr_in *peer, int fd, |
| 1313 | void *pack, unsigned len) |
| 1314 | { |
| 1315 | struct pdp_t *pdp, *pdp_old; |
| 1316 | struct pdp_t pdp_buf; |
| 1317 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1318 | uint8_t recovery; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1319 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1320 | uint16_t seq = get_seq(pack); |
| 1321 | int hlen = get_hlen(pack); |
| 1322 | uint8_t linked_nsapi = 0; |
| 1323 | struct pdp_t *linked_pdp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1324 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1325 | if (!gtp_dublicate(gsn, version, peer, seq)) |
| 1326 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1327 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1328 | pdp = &pdp_buf; |
| 1329 | memset(pdp, 0, sizeof(struct pdp_t)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1330 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1331 | if (version == 0) { |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 1332 | uint64_t tid = be64toh(((union gtp_packet *)pack)->gtp0.h.tid); |
| 1333 | |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame^] | 1334 | pdp_set_imsi_nsapi(pdp, tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1335 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1336 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1337 | pdp->seq = seq; |
| 1338 | pdp->sa_peer = *peer; |
| 1339 | pdp->fd = fd; |
| 1340 | pdp->version = version; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1341 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1342 | /* Decode information elements */ |
| 1343 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 1344 | gsn->invalid++; |
| 1345 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 1346 | "Invalid message format"); |
| 1347 | if (0 == version) |
| 1348 | return EOF; |
| 1349 | else |
| 1350 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1351 | GTPCAUSE_INVALID_MESSAGE); |
| 1352 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1353 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1354 | if (version == 1) { |
| 1355 | /* Linked NSAPI (conditional) */ |
| 1356 | /* If included this is the Secondary PDP Context Activation Procedure */ |
| 1357 | /* In secondary activation IMSI is not included, so the context must be */ |
| 1358 | /* identified by the tei */ |
| 1359 | if (!gtpie_gettv1(ie, GTPIE_NSAPI, 1, &linked_nsapi)) { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1360 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1361 | /* Find the primary PDP context */ |
| 1362 | if (pdp_getgtp1(&linked_pdp, get_tei(pack))) { |
| 1363 | gsn->incorrect++; |
| 1364 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 1365 | pack, len, |
| 1366 | "Incorrect optional information field"); |
| 1367 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1368 | GTPCAUSE_OPT_IE_INCORRECT); |
| 1369 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1370 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1371 | /* Check that the primary PDP context matches linked nsapi */ |
| 1372 | if (linked_pdp->nsapi != linked_nsapi) { |
| 1373 | gsn->incorrect++; |
| 1374 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 1375 | pack, len, |
| 1376 | "Incorrect optional information field"); |
| 1377 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1378 | GTPCAUSE_OPT_IE_INCORRECT); |
| 1379 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1380 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1381 | /* Copy parameters from primary context */ |
| 1382 | pdp->selmode = linked_pdp->selmode; |
| 1383 | pdp->imsi = linked_pdp->imsi; |
| 1384 | pdp->msisdn = linked_pdp->msisdn; |
| 1385 | pdp->eua = linked_pdp->eua; |
| 1386 | pdp->pco_req = linked_pdp->pco_req; |
| 1387 | pdp->apn_req = linked_pdp->apn_req; |
| 1388 | pdp->teic_gn = linked_pdp->teic_gn; |
| 1389 | pdp->secondary = 1; |
| 1390 | } |
| 1391 | } |
| 1392 | /* if (version == 1) */ |
| 1393 | if (version == 0) { |
| 1394 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 1395 | pdp->qos_req0, sizeof(pdp->qos_req0))) { |
| 1396 | gsn->missing++; |
| 1397 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1398 | len, "Missing mandatory information field"); |
| 1399 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1400 | GTPCAUSE_MAN_IE_MISSING); |
| 1401 | } |
| 1402 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1403 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1404 | if ((version == 1) && (!linked_pdp)) { |
| 1405 | /* Not Secondary PDP Context Activation Procedure */ |
| 1406 | /* IMSI (conditional) */ |
| 1407 | if (gtpie_gettv0 |
| 1408 | (ie, GTPIE_IMSI, 0, &pdp->imsi, sizeof(pdp->imsi))) { |
| 1409 | gsn->missing++; |
| 1410 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1411 | len, "Missing mandatory information field"); |
| 1412 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1413 | GTPCAUSE_MAN_IE_MISSING); |
| 1414 | } |
| 1415 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1416 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1417 | /* Recovery (optional) */ |
| 1418 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 1419 | if (gsn->cb_recovery) |
| 1420 | gsn->cb_recovery(peer, recovery); |
| 1421 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1422 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1423 | /* Selection mode (conditional) */ |
| 1424 | if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */ |
| 1425 | if (gtpie_gettv0(ie, GTPIE_SELECTION_MODE, 0, |
| 1426 | &pdp->selmode, sizeof(pdp->selmode))) { |
| 1427 | gsn->missing++; |
| 1428 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1429 | len, "Missing mandatory information field"); |
| 1430 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1431 | GTPCAUSE_MAN_IE_MISSING); |
| 1432 | } |
| 1433 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1434 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1435 | if (version == 0) { |
| 1436 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 1437 | gsn->missing++; |
| 1438 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1439 | len, "Missing mandatory information field"); |
| 1440 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1441 | GTPCAUSE_MAN_IE_MISSING); |
| 1442 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1443 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1444 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 1445 | gsn->missing++; |
| 1446 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1447 | len, "Missing mandatory information field"); |
| 1448 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1449 | GTPCAUSE_MAN_IE_MISSING); |
| 1450 | } |
| 1451 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1452 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1453 | if (version == 1) { |
| 1454 | /* TEID (mandatory) */ |
| 1455 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 1456 | gsn->missing++; |
| 1457 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1458 | len, "Missing mandatory information field"); |
| 1459 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1460 | GTPCAUSE_MAN_IE_MISSING); |
| 1461 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1462 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1463 | /* TEIC (conditional) */ |
| 1464 | if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */ |
| 1465 | if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) { |
| 1466 | gsn->missing++; |
| 1467 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 1468 | pack, len, |
| 1469 | "Missing mandatory information field"); |
| 1470 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1471 | GTPCAUSE_MAN_IE_MISSING); |
| 1472 | } |
| 1473 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1474 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1475 | /* NSAPI (mandatory) */ |
| 1476 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) { |
| 1477 | gsn->missing++; |
| 1478 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1479 | len, "Missing mandatory information field"); |
| 1480 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1481 | GTPCAUSE_MAN_IE_MISSING); |
| 1482 | } |
| 1483 | } |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 1484 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1485 | /* Charging Characteriatics (optional) */ |
| 1486 | /* Trace reference (optional) */ |
| 1487 | /* Trace type (optional) */ |
| 1488 | /* Charging Characteriatics (optional) */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1489 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1490 | if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */ |
| 1491 | /* End User Address (conditional) */ |
| 1492 | if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 1493 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
| 1494 | gsn->missing++; |
| 1495 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1496 | len, "Missing mandatory information field"); |
| 1497 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1498 | GTPCAUSE_MAN_IE_MISSING); |
| 1499 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1500 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1501 | /* APN */ |
| 1502 | if (gtpie_gettlv(ie, GTPIE_APN, 0, &pdp->apn_req.l, |
| 1503 | &pdp->apn_req.v, sizeof(pdp->apn_req.v))) { |
| 1504 | gsn->missing++; |
| 1505 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1506 | len, "Missing mandatory information field"); |
| 1507 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1508 | GTPCAUSE_MAN_IE_MISSING); |
| 1509 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1510 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1511 | /* Extract protocol configuration options (optional) */ |
| 1512 | if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l, |
| 1513 | &pdp->pco_req.v, sizeof(pdp->pco_req.v))) { |
| 1514 | } |
| 1515 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1516 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1517 | /* SGSN address for signalling (mandatory) */ |
| 1518 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 1519 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 1520 | gsn->missing++; |
| 1521 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 1522 | "Missing mandatory information field"); |
| 1523 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1524 | GTPCAUSE_MAN_IE_MISSING); |
| 1525 | } |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 1526 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1527 | /* SGSN address for user traffic (mandatory) */ |
| 1528 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 1529 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 1530 | gsn->missing++; |
| 1531 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 1532 | "Missing mandatory information field"); |
| 1533 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1534 | GTPCAUSE_MAN_IE_MISSING); |
| 1535 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1536 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1537 | if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */ |
| 1538 | /* MSISDN (conditional) */ |
| 1539 | if (gtpie_gettlv(ie, GTPIE_MSISDN, 0, &pdp->msisdn.l, |
| 1540 | &pdp->msisdn.v, sizeof(pdp->msisdn.v))) { |
| 1541 | gsn->missing++; |
| 1542 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1543 | len, "Missing mandatory information field"); |
| 1544 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1545 | GTPCAUSE_MAN_IE_MISSING); |
| 1546 | } |
| 1547 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1548 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1549 | if (version == 1) { |
| 1550 | /* QoS (mandatory) */ |
| 1551 | if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l, |
| 1552 | &pdp->qos_req.v, sizeof(pdp->qos_req.v))) { |
| 1553 | gsn->missing++; |
| 1554 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1555 | len, "Missing mandatory information field"); |
| 1556 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1557 | GTPCAUSE_MAN_IE_MISSING); |
| 1558 | } |
| 1559 | |
| 1560 | /* TFT (conditional) */ |
| 1561 | if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l, |
| 1562 | &pdp->tft.v, sizeof(pdp->tft.v))) { |
| 1563 | } |
| 1564 | |
| 1565 | /* Trigger ID */ |
| 1566 | /* OMC identity */ |
| 1567 | } |
| 1568 | |
| 1569 | /* Initialize our own IP addresses */ |
| 1570 | in_addr2gsna(&pdp->gsnlc, &gsn->gsnc); |
| 1571 | in_addr2gsna(&pdp->gsnlu, &gsn->gsnu); |
| 1572 | |
| 1573 | if (GTP_DEBUG) |
| 1574 | printf("gtp_create_pdp_ind: Before pdp_tidget\n"); |
| 1575 | |
| 1576 | if (!pdp_getimsi(&pdp_old, pdp->imsi, pdp->nsapi)) { |
| 1577 | /* Found old pdp with same tid. Now the voodoo begins! */ |
| 1578 | /* 09.60 / 29.060 allows create on existing context to "steal" */ |
| 1579 | /* the context which was allready established */ |
| 1580 | /* We check that the APN, selection mode and MSISDN is the same */ |
| 1581 | if (GTP_DEBUG) |
| 1582 | printf("gtp_create_pdp_ind: Old context found\n"); |
| 1583 | if ((pdp->apn_req.l == pdp_old->apn_req.l) |
| 1584 | && |
| 1585 | (!memcmp |
| 1586 | (pdp->apn_req.v, pdp_old->apn_req.v, pdp->apn_req.l)) |
| 1587 | && (pdp->selmode == pdp_old->selmode) |
| 1588 | && (pdp->msisdn.l == pdp_old->msisdn.l) |
| 1589 | && |
| 1590 | (!memcmp(pdp->msisdn.v, pdp_old->msisdn.v, pdp->msisdn.l))) |
| 1591 | { |
| 1592 | /* OK! We are dealing with the same APN. We will copy new |
| 1593 | * parameters to the old pdp and send off confirmation |
| 1594 | * We ignore the following information elements: |
| 1595 | * QoS: MS will get originally negotiated QoS. |
| 1596 | * End user address (EUA). MS will get old EUA anyway. |
| 1597 | * Protocol configuration option (PCO): Only application can verify */ |
| 1598 | |
| 1599 | if (GTP_DEBUG) |
| 1600 | printf |
| 1601 | ("gtp_create_pdp_ind: Old context found\n"); |
| 1602 | |
| 1603 | /* Copy remote flow label */ |
| 1604 | pdp_old->flru = pdp->flru; |
| 1605 | pdp_old->flrc = pdp->flrc; |
| 1606 | |
| 1607 | /* Copy remote tei */ |
| 1608 | pdp_old->teid_gn = pdp->teid_gn; |
| 1609 | pdp_old->teic_gn = pdp->teic_gn; |
| 1610 | |
| 1611 | /* Copy peer GSN address */ |
| 1612 | pdp_old->gsnrc.l = pdp->gsnrc.l; |
| 1613 | memcpy(&pdp_old->gsnrc.v, &pdp->gsnrc.v, pdp->gsnrc.l); |
| 1614 | pdp_old->gsnru.l = pdp->gsnru.l; |
| 1615 | memcpy(&pdp_old->gsnru.v, &pdp->gsnru.v, pdp->gsnru.l); |
| 1616 | |
| 1617 | /* Copy request parameters */ |
| 1618 | pdp_old->seq = pdp->seq; |
| 1619 | pdp_old->sa_peer = pdp->sa_peer; |
| 1620 | pdp_old->fd = pdp->fd = fd; |
| 1621 | pdp_old->version = pdp->version = version; |
| 1622 | |
| 1623 | /* Switch to using the old pdp context */ |
| 1624 | pdp = pdp_old; |
| 1625 | |
| 1626 | /* Confirm to peer that things were "successful" */ |
| 1627 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1628 | GTPCAUSE_ACC_REQ); |
| 1629 | } else { /* This is not the same PDP context. Delete the old one. */ |
| 1630 | |
| 1631 | if (GTP_DEBUG) |
| 1632 | printf |
| 1633 | ("gtp_create_pdp_ind: Deleting old context\n"); |
| 1634 | |
| 1635 | if (gsn->cb_delete_context) |
| 1636 | gsn->cb_delete_context(pdp_old); |
| 1637 | pdp_freepdp(pdp_old); |
| 1638 | |
| 1639 | if (GTP_DEBUG) |
| 1640 | printf("gtp_create_pdp_ind: Deleted...\n"); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | pdp_newpdp(&pdp, pdp->imsi, pdp->nsapi, pdp); |
| 1645 | |
| 1646 | /* Callback function to validata login */ |
| 1647 | if (gsn->cb_create_context_ind != 0) |
| 1648 | return gsn->cb_create_context_ind(pdp); |
| 1649 | else { |
| 1650 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 1651 | "No create_context_ind callback defined"); |
| 1652 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1653 | GTPCAUSE_NOT_SUPPORTED); |
| 1654 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1657 | /* Handle Create PDP Context Response */ |
| 1658 | int gtp_create_pdp_conf(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1659 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 1660 | { |
| 1661 | struct pdp_t *pdp; |
| 1662 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1663 | uint8_t cause, recovery; |
| 1664 | void *cbp = NULL; |
| 1665 | uint8_t type = 0; |
| 1666 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1667 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1668 | /* Remove packet from queue */ |
| 1669 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) |
| 1670 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1671 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1672 | /* Find the context in question */ |
| 1673 | if (pdp_getgtp1(&pdp, get_tei(pack))) { |
| 1674 | gsn->err_unknownpdp++; |
| 1675 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 1676 | "Unknown PDP context"); |
| 1677 | if (gsn->cb_conf) |
| 1678 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 1679 | return EOF; |
| 1680 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1681 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1682 | /* Register that we have received a valid teic from GGSN */ |
| 1683 | pdp->teic_confirmed = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1684 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1685 | /* Decode information elements */ |
| 1686 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 1687 | gsn->invalid++; |
| 1688 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 1689 | "Invalid message format"); |
| 1690 | if (gsn->cb_conf) |
| 1691 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1692 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1693 | pdp_freepdp(pdp); */ |
| 1694 | return EOF; |
| 1695 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1696 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1697 | /* Extract cause value (mandatory) */ |
| 1698 | if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) { |
| 1699 | gsn->missing++; |
| 1700 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 1701 | "Missing mandatory information field"); |
| 1702 | if (gsn->cb_conf) |
| 1703 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1704 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1705 | pdp_freepdp(pdp); */ |
| 1706 | return EOF; |
| 1707 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1708 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1709 | /* Extract recovery (optional) */ |
| 1710 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 1711 | if (gsn->cb_recovery) |
| 1712 | gsn->cb_recovery(peer, recovery); |
| 1713 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1714 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1715 | /* Extract protocol configuration options (optional) */ |
| 1716 | if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l, |
| 1717 | &pdp->pco_req.v, sizeof(pdp->pco_req.v))) { |
| 1718 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1719 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1720 | /* Check all conditional information elements */ |
| 1721 | if (GTPCAUSE_ACC_REQ == cause) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1722 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1723 | if (version == 0) { |
| 1724 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 1725 | &pdp->qos_neg0, |
| 1726 | sizeof(pdp->qos_neg0))) { |
| 1727 | gsn->missing++; |
| 1728 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 1729 | pack, len, |
| 1730 | "Missing conditional information field"); |
| 1731 | if (gsn->cb_conf) |
| 1732 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1733 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1734 | pdp_freepdp(pdp); */ |
| 1735 | return EOF; |
| 1736 | } |
| 1737 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1738 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1739 | if (gtpie_gettv1(ie, GTPIE_REORDER, 0, &pdp->reorder)) { |
| 1740 | gsn->missing++; |
| 1741 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1742 | len, |
| 1743 | "Missing conditional information field"); |
| 1744 | if (gsn->cb_conf) |
| 1745 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1746 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1747 | pdp_freepdp(pdp); */ |
| 1748 | return EOF; |
| 1749 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1750 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1751 | if (version == 0) { |
| 1752 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 1753 | gsn->missing++; |
| 1754 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 1755 | pack, len, |
| 1756 | "Missing conditional information field"); |
| 1757 | if (gsn->cb_conf) |
| 1758 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1759 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1760 | pdp_freepdp(pdp); */ |
| 1761 | return EOF; |
| 1762 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1763 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1764 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 1765 | gsn->missing++; |
| 1766 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 1767 | pack, len, |
| 1768 | "Missing conditional information field"); |
| 1769 | if (gsn->cb_conf) |
| 1770 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1771 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1772 | pdp_freepdp(pdp); */ |
| 1773 | return EOF; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | if (version == 1) { |
| 1778 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 1779 | gsn->missing++; |
| 1780 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 1781 | pack, len, |
| 1782 | "Missing conditional information field"); |
| 1783 | if (gsn->cb_conf) |
| 1784 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1785 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1786 | pdp_freepdp(pdp); */ |
| 1787 | return EOF; |
| 1788 | } |
| 1789 | |
| 1790 | if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) { |
| 1791 | gsn->missing++; |
| 1792 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 1793 | pack, len, |
| 1794 | "Missing conditional information field"); |
| 1795 | if (gsn->cb_conf) |
| 1796 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1797 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1798 | pdp_freepdp(pdp); */ |
| 1799 | return EOF; |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | if (gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid)) { |
| 1804 | gsn->missing++; |
| 1805 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1806 | len, |
| 1807 | "Missing conditional information field"); |
| 1808 | if (gsn->cb_conf) |
| 1809 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1810 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1811 | pdp_freepdp(pdp); */ |
| 1812 | } |
| 1813 | |
| 1814 | if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 1815 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
| 1816 | gsn->missing++; |
| 1817 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1818 | len, |
| 1819 | "Missing conditional information field"); |
| 1820 | if (gsn->cb_conf) |
| 1821 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1822 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1823 | pdp_freepdp(pdp); */ |
| 1824 | return EOF; |
| 1825 | } |
| 1826 | |
| 1827 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 1828 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 1829 | gsn->missing++; |
| 1830 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1831 | len, |
| 1832 | "Missing conditional information field"); |
| 1833 | if (gsn->cb_conf) |
| 1834 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1835 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1836 | pdp_freepdp(pdp); */ |
| 1837 | return EOF; |
| 1838 | } |
| 1839 | |
| 1840 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 1841 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 1842 | gsn->missing++; |
| 1843 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 1844 | len, |
| 1845 | "Missing conditional information field"); |
| 1846 | if (gsn->cb_conf) |
| 1847 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1848 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1849 | pdp_freepdp(pdp); */ |
| 1850 | return EOF; |
| 1851 | } |
| 1852 | |
| 1853 | if (version == 1) { |
| 1854 | if (gtpie_gettlv |
| 1855 | (ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_neg.l, |
| 1856 | &pdp->qos_neg.v, sizeof(pdp->qos_neg.v))) { |
| 1857 | gsn->missing++; |
| 1858 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 1859 | pack, len, |
| 1860 | "Missing conditional information field"); |
| 1861 | if (gsn->cb_conf) |
| 1862 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1863 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1864 | pdp_freepdp(pdp); */ |
| 1865 | return EOF; |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | } |
| 1870 | |
| 1871 | if (gsn->cb_conf) |
| 1872 | gsn->cb_conf(type, cause, pdp, cbp); |
| 1873 | |
| 1874 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1877 | /* API: Send Update PDP Context Request */ |
| 1878 | int gtp_update_context(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1879 | struct in_addr *inetaddr) |
| 1880 | { |
| 1881 | union gtp_packet packet; |
| 1882 | unsigned int length = |
| 1883 | get_default_gtp(pdp->version, GTP_UPDATE_PDP_REQ, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1884 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1885 | if (pdp->version == 0) |
| 1886 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1887 | sizeof(pdp->qos_req0), pdp->qos_req0); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1888 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1889 | /* Include IMSI if updating with unknown teic_gn */ |
| 1890 | if ((pdp->version == 1) && (!pdp->teic_gn)) |
| 1891 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI, |
| 1892 | sizeof(pdp->imsi), (uint8_t *) & pdp->imsi); |
| 1893 | |
| 1894 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1895 | gsn->restart_counter); |
| 1896 | |
| 1897 | if (pdp->version == 0) { |
| 1898 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, pdp->fllu); |
| 1899 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, pdp->fllc); |
| 1900 | } |
| 1901 | |
| 1902 | if (pdp->version == 1) { |
| 1903 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1904 | pdp->teid_own); |
| 1905 | |
| 1906 | if (!pdp->teic_confirmed) |
| 1907 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1908 | pdp->teic_own); |
| 1909 | } |
| 1910 | |
| 1911 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, pdp->nsapi); |
| 1912 | |
| 1913 | /* TODO |
| 1914 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_REF, |
| 1915 | pdp->traceref); |
| 1916 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_TYPE, |
| 1917 | pdp->tracetype); */ |
| 1918 | |
| 1919 | /* TODO if ggsn update message |
| 1920 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1921 | pdp->eua.l, pdp->eua.v); |
| 1922 | */ |
| 1923 | |
| 1924 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1925 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 1926 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1927 | pdp->gsnlu.l, pdp->gsnlu.v); |
| 1928 | |
| 1929 | if (pdp->version == 1) |
| 1930 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1931 | pdp->qos_req.l, pdp->qos_req.v); |
| 1932 | |
| 1933 | if ((pdp->version == 1) && pdp->tft.l) |
| 1934 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TFT, |
| 1935 | pdp->tft.l, pdp->tft.v); |
| 1936 | |
| 1937 | if ((pdp->version == 1) && pdp->triggerid.l) |
| 1938 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TRIGGER_ID, |
| 1939 | pdp->triggerid.l, pdp->triggerid.v); |
| 1940 | |
| 1941 | if ((pdp->version == 1) && pdp->omcid.l) |
| 1942 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_OMC_ID, |
| 1943 | pdp->omcid.l, pdp->omcid.v); |
| 1944 | |
| 1945 | gtp_req(gsn, pdp->version, NULL, &packet, length, inetaddr, cbp); |
| 1946 | |
| 1947 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1950 | /* Send Update PDP Context Response */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1951 | int gtp_update_pdp_resp(struct gsn_t *gsn, int version, |
| 1952 | struct sockaddr_in *peer, int fd, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1953 | void *pack, unsigned len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1954 | struct pdp_t *pdp, uint8_t cause) |
| 1955 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1956 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1957 | union gtp_packet packet; |
| 1958 | unsigned int length = |
| 1959 | get_default_gtp(version, GTP_UPDATE_PDP_RSP, &packet); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1960 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1961 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1962 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1963 | if (cause == GTPCAUSE_ACC_REQ) { |
| 1964 | |
| 1965 | if (version == 0) |
| 1966 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1967 | sizeof(pdp->qos_neg0), pdp->qos_neg0); |
| 1968 | |
| 1969 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1970 | gsn->restart_counter); |
| 1971 | |
| 1972 | if (version == 0) { |
| 1973 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, |
| 1974 | pdp->fllu); |
| 1975 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, |
| 1976 | pdp->fllc); |
| 1977 | } |
| 1978 | |
| 1979 | if (version == 1) { |
| 1980 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1981 | pdp->teid_own); |
| 1982 | |
| 1983 | if (!pdp->teic_confirmed) |
| 1984 | gtpie_tv4(&packet, &length, GTP_MAX, |
| 1985 | GTPIE_TEI_C, pdp->teic_own); |
| 1986 | } |
| 1987 | |
| 1988 | /* TODO we use teid_own as charging ID address */ |
| 1989 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID, |
| 1990 | pdp->teid_own); |
| 1991 | |
| 1992 | /* If ggsn |
| 1993 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1994 | pdp->eua.l, pdp->eua.v); */ |
| 1995 | |
| 1996 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1997 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 1998 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1999 | pdp->gsnlu.l, pdp->gsnlu.v); |
| 2000 | |
| 2001 | if (version == 1) |
| 2002 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 2003 | pdp->qos_neg.l, pdp->qos_neg.v); |
| 2004 | |
| 2005 | /* TODO: Charging gateway address */ |
| 2006 | } |
| 2007 | |
| 2008 | return gtp_resp(version, gsn, pdp, &packet, length, peer, |
| 2009 | fd, get_seq(pack), get_tid(pack)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2012 | /* Handle Update PDP Context Request */ |
| 2013 | int gtp_update_pdp_ind(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2014 | struct sockaddr_in *peer, int fd, |
| 2015 | void *pack, unsigned len) |
| 2016 | { |
| 2017 | struct pdp_t *pdp; |
| 2018 | struct pdp_t pdp_backup; |
| 2019 | union gtpie_member *ie[GTPIE_SIZE]; |
| 2020 | uint8_t recovery; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2021 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2022 | uint16_t seq = get_seq(pack); |
| 2023 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2024 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2025 | uint64_t imsi; |
| 2026 | uint8_t nsapi; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2027 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2028 | /* Is this a dublicate ? */ |
| 2029 | if (!gtp_dublicate(gsn, version, peer, seq)) { |
| 2030 | return 0; /* We allready send of response once */ |
| 2031 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2032 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2033 | /* Decode information elements */ |
| 2034 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2035 | gsn->invalid++; |
| 2036 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2037 | "Invalid message format"); |
| 2038 | if (0 == version) |
| 2039 | return EOF; |
| 2040 | else |
| 2041 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2042 | len, NULL, |
| 2043 | GTPCAUSE_INVALID_MESSAGE); |
| 2044 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2045 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2046 | /* Finding PDP: */ |
| 2047 | /* For GTP0 we use the tunnel identifier to provide imsi and nsapi. */ |
| 2048 | /* For GTP1 we must use imsi and nsapi if imsi is present. Otherwise */ |
| 2049 | /* we have to use the tunnel endpoint identifier */ |
| 2050 | if (version == 0) { |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 2051 | uint64_t tid = be64toh(((union gtp_packet *)pack)->gtp0.h.tid); |
| 2052 | |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame^] | 2053 | pdp_set_imsi_nsapi(pdp, tid); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2054 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2055 | /* Find the context in question */ |
| 2056 | if (pdp_getimsi(&pdp, imsi, nsapi)) { |
| 2057 | gsn->err_unknownpdp++; |
| 2058 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2059 | len, "Unknown PDP context"); |
| 2060 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2061 | len, NULL, |
| 2062 | GTPCAUSE_NON_EXIST); |
| 2063 | } |
| 2064 | } else if (version == 1) { |
| 2065 | /* NSAPI (mandatory) */ |
| 2066 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) { |
| 2067 | gsn->missing++; |
| 2068 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2069 | len, "Missing mandatory information field"); |
| 2070 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2071 | len, NULL, |
| 2072 | GTPCAUSE_MAN_IE_MISSING); |
| 2073 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2074 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2075 | /* IMSI (conditional) */ |
| 2076 | if (gtpie_gettv0(ie, GTPIE_IMSI, 0, &imsi, sizeof(imsi))) { |
| 2077 | /* Find the context in question */ |
| 2078 | if (pdp_getgtp1(&pdp, get_tei(pack))) { |
| 2079 | gsn->err_unknownpdp++; |
| 2080 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 2081 | pack, len, "Unknown PDP context"); |
| 2082 | return gtp_update_pdp_resp(gsn, version, peer, |
| 2083 | fd, pack, len, NULL, |
| 2084 | GTPCAUSE_NON_EXIST); |
| 2085 | } |
| 2086 | } else { |
| 2087 | /* Find the context in question */ |
| 2088 | if (pdp_getimsi(&pdp, imsi, nsapi)) { |
| 2089 | gsn->err_unknownpdp++; |
| 2090 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, |
| 2091 | pack, len, "Unknown PDP context"); |
| 2092 | return gtp_update_pdp_resp(gsn, version, peer, |
| 2093 | fd, pack, len, NULL, |
| 2094 | GTPCAUSE_NON_EXIST); |
| 2095 | } |
| 2096 | } |
| 2097 | } else { |
| 2098 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown version"); |
| 2099 | return EOF; |
| 2100 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2101 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2102 | /* Make a backup copy in case anything is wrong */ |
| 2103 | memcpy(&pdp_backup, pdp, sizeof(pdp_backup)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2104 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2105 | if (version == 0) { |
| 2106 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 2107 | pdp->qos_req0, sizeof(pdp->qos_req0))) { |
| 2108 | gsn->missing++; |
| 2109 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2110 | len, "Missing mandatory information field"); |
| 2111 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2112 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2113 | len, pdp, |
| 2114 | GTPCAUSE_MAN_IE_MISSING); |
| 2115 | } |
| 2116 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2117 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2118 | /* Recovery (optional) */ |
| 2119 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 2120 | if (gsn->cb_recovery) |
| 2121 | gsn->cb_recovery(peer, recovery); |
| 2122 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2123 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2124 | if (version == 0) { |
| 2125 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 2126 | gsn->missing++; |
| 2127 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2128 | len, "Missing mandatory information field"); |
| 2129 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2130 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2131 | len, pdp, |
| 2132 | GTPCAUSE_MAN_IE_MISSING); |
| 2133 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2134 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2135 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 2136 | gsn->missing++; |
| 2137 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2138 | len, "Missing mandatory information field"); |
| 2139 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2140 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2141 | len, pdp, |
| 2142 | GTPCAUSE_MAN_IE_MISSING); |
| 2143 | } |
| 2144 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2145 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2146 | if (version == 1) { |
| 2147 | /* TEID (mandatory) */ |
| 2148 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 2149 | gsn->missing++; |
| 2150 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2151 | len, "Missing mandatory information field"); |
| 2152 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2153 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2154 | len, pdp, |
| 2155 | GTPCAUSE_MAN_IE_MISSING); |
| 2156 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2157 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2158 | /* TEIC (conditional) */ |
| 2159 | /* If TEIC is not included it means that we have allready received it */ |
| 2160 | /* TODO: From 29.060 it is not clear if TEI_C MUST be included for */ |
| 2161 | /* all updated contexts, or only for one of the linked contexts */ |
| 2162 | gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn); |
| 2163 | |
| 2164 | /* NSAPI (mandatory) */ |
| 2165 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) { |
| 2166 | gsn->missing++; |
| 2167 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2168 | len, "Missing mandatory information field"); |
| 2169 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2170 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2171 | len, pdp, |
| 2172 | GTPCAUSE_MAN_IE_MISSING); |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | /* Trace reference (optional) */ |
| 2177 | /* Trace type (optional) */ |
| 2178 | |
| 2179 | /* End User Address (conditional) TODO: GGSN Initiated |
| 2180 | if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 2181 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
| 2182 | gsn->missing++; |
| 2183 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2184 | "Missing mandatory information field"); |
| 2185 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2186 | return gtp_update_pdp_resp(gsn, version, pdp, |
| 2187 | GTPCAUSE_MAN_IE_MISSING); |
| 2188 | } */ |
| 2189 | |
| 2190 | /* SGSN address for signalling (mandatory) */ |
| 2191 | /* It is weird that this is mandatory when TEIC is conditional */ |
| 2192 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 2193 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 2194 | gsn->missing++; |
| 2195 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2196 | "Missing mandatory information field"); |
| 2197 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2198 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2199 | pdp, GTPCAUSE_MAN_IE_MISSING); |
| 2200 | } |
| 2201 | |
| 2202 | /* SGSN address for user traffic (mandatory) */ |
| 2203 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 2204 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 2205 | gsn->missing++; |
| 2206 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2207 | "Missing mandatory information field"); |
| 2208 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2209 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2210 | pdp, GTPCAUSE_MAN_IE_MISSING); |
| 2211 | } |
| 2212 | |
| 2213 | if (version == 1) { |
| 2214 | /* QoS (mandatory) */ |
| 2215 | if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l, |
| 2216 | &pdp->qos_req.v, sizeof(pdp->qos_req.v))) { |
| 2217 | gsn->missing++; |
| 2218 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2219 | len, "Missing mandatory information field"); |
| 2220 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2221 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2222 | len, pdp, |
| 2223 | GTPCAUSE_MAN_IE_MISSING); |
| 2224 | } |
| 2225 | |
| 2226 | /* TFT (conditional) */ |
| 2227 | if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l, |
| 2228 | &pdp->tft.v, sizeof(pdp->tft.v))) { |
| 2229 | } |
| 2230 | |
| 2231 | /* OMC identity */ |
| 2232 | } |
| 2233 | |
| 2234 | /* Confirm to peer that things were "successful" */ |
| 2235 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp, |
| 2236 | GTPCAUSE_ACC_REQ); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2239 | /* Handle Update PDP Context Response */ |
| 2240 | int gtp_update_pdp_conf(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2241 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 2242 | { |
| 2243 | struct pdp_t *pdp; |
| 2244 | union gtpie_member *ie[GTPIE_SIZE]; |
| 2245 | uint8_t cause, recovery; |
| 2246 | void *cbp = NULL; |
| 2247 | uint8_t type = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2248 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2249 | /* Remove packet from queue */ |
| 2250 | if (gtp_conf(gsn, 0, peer, pack, len, &type, &cbp)) |
| 2251 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2252 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2253 | /* Find the context in question */ |
| 2254 | if (pdp_getgtp0(&pdp, ntoh16(((union gtp_packet *)pack)->gtp0.h.flow))) { |
| 2255 | gsn->err_unknownpdp++; |
| 2256 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2257 | "Unknown PDP context"); |
| 2258 | if (gsn->cb_conf) |
| 2259 | gsn->cb_conf(type, cause, NULL, cbp); |
| 2260 | return EOF; |
| 2261 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2262 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2263 | /* Register that we have received a valid teic from GGSN */ |
| 2264 | pdp->teic_confirmed = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2265 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2266 | /* Decode information elements */ |
| 2267 | if (gtpie_decaps |
| 2268 | (ie, 0, pack + GTP0_HEADER_SIZE, len - GTP0_HEADER_SIZE)) { |
| 2269 | gsn->invalid++; |
| 2270 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2271 | "Invalid message format"); |
| 2272 | if (gsn->cb_conf) |
| 2273 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 2274 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 2275 | pdp_freepdp(pdp); */ |
| 2276 | return EOF; |
| 2277 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2278 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2279 | /* Extract cause value (mandatory) */ |
| 2280 | if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) { |
| 2281 | gsn->missing++; |
| 2282 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2283 | "Missing mandatory information field"); |
| 2284 | if (gsn->cb_conf) |
| 2285 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 2286 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 2287 | pdp_freepdp(pdp); */ |
| 2288 | return EOF; |
| 2289 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2290 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2291 | /* Extract recovery (optional) */ |
| 2292 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 2293 | if (gsn->cb_recovery) |
| 2294 | gsn->cb_recovery(peer, recovery); |
| 2295 | } |
| 2296 | |
| 2297 | /* Check all conditional information elements */ |
| 2298 | if (GTPCAUSE_ACC_REQ != cause) { |
| 2299 | if (gsn->cb_conf) |
| 2300 | gsn->cb_conf(type, cause, pdp, cbp); |
| 2301 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 2302 | pdp_freepdp(pdp); */ |
| 2303 | return 0; |
| 2304 | } else { |
| 2305 | /* Check for missing conditionary information elements */ |
| 2306 | if (!(gtpie_exist(ie, GTPIE_QOS_PROFILE0, 0) && |
| 2307 | gtpie_exist(ie, GTPIE_REORDER, 0) && |
| 2308 | gtpie_exist(ie, GTPIE_FL_DI, 0) && |
| 2309 | gtpie_exist(ie, GTPIE_FL_C, 0) && |
| 2310 | gtpie_exist(ie, GTPIE_CHARGING_ID, 0) && |
| 2311 | gtpie_exist(ie, GTPIE_EUA, 0) && |
| 2312 | gtpie_exist(ie, GTPIE_GSN_ADDR, 0) && |
| 2313 | gtpie_exist(ie, GTPIE_GSN_ADDR, 1))) { |
| 2314 | gsn->missing++; |
| 2315 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2316 | len, |
| 2317 | "Missing conditional information field"); |
| 2318 | if (gsn->cb_conf) |
| 2319 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 2320 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 2321 | pdp_freepdp(pdp); */ |
| 2322 | return EOF; |
| 2323 | } |
| 2324 | |
| 2325 | /* Update pdp with new values */ |
| 2326 | gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 2327 | pdp->qos_neg0, sizeof(pdp->qos_neg0)); |
| 2328 | gtpie_gettv1(ie, GTPIE_REORDER, 0, &pdp->reorder); |
| 2329 | gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru); |
| 2330 | gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc); |
| 2331 | gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid); |
| 2332 | gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 2333 | &pdp->eua.v, sizeof(pdp->eua.v)); |
| 2334 | gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 2335 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v)); |
| 2336 | gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 2337 | &pdp->gsnru.v, sizeof(pdp->gsnru.v)); |
| 2338 | |
| 2339 | if (gsn->cb_conf) |
| 2340 | gsn->cb_conf(type, cause, pdp, cbp); |
| 2341 | return 0; /* Succes */ |
| 2342 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2345 | /* API: Send Delete PDP Context Request */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2346 | int gtp_delete_context_req(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2347 | int teardown) |
| 2348 | { |
| 2349 | union gtp_packet packet; |
| 2350 | unsigned int length = |
| 2351 | get_default_gtp(pdp->version, GTP_DELETE_PDP_REQ, &packet); |
| 2352 | struct in_addr addr; |
| 2353 | struct pdp_t *linked_pdp; |
| 2354 | struct pdp_t *secondary_pdp; |
| 2355 | int n; |
| 2356 | int count = 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2357 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2358 | if (gsna2in_addr(&addr, &pdp->gsnrc)) { |
| 2359 | gsn->err_address++; |
| 2360 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 2361 | "GSN address conversion failed"); |
| 2362 | return EOF; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2363 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2364 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2365 | if (pdp_getgtp1(&linked_pdp, pdp->teic_own)) { |
| 2366 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 2367 | "Unknown linked PDP context"); |
| 2368 | return EOF; |
| 2369 | } |
| 2370 | |
| 2371 | if (!teardown) { |
| 2372 | for (n = 0; n < PDP_MAXNSAPI; n++) |
| 2373 | if (linked_pdp->secondary_tei[n]) |
| 2374 | count++; |
| 2375 | if (count <= 1) { |
| 2376 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 2377 | "Must use teardown for last context"); |
| 2378 | return EOF; |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | if (pdp->version == 1) { |
| 2383 | if (teardown) |
| 2384 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_TEARDOWN, |
| 2385 | 0xff); |
| 2386 | |
| 2387 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, pdp->nsapi); |
| 2388 | } |
| 2389 | |
| 2390 | gtp_req(gsn, pdp->version, pdp, &packet, length, &addr, cbp); |
| 2391 | |
| 2392 | if (teardown) { /* Remove all contexts */ |
| 2393 | for (n = 0; n < PDP_MAXNSAPI; n++) { |
| 2394 | if (linked_pdp->secondary_tei[n]) { |
| 2395 | if (pdp_getgtp1 |
| 2396 | (&secondary_pdp, |
| 2397 | linked_pdp->secondary_tei[n])) { |
| 2398 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 2399 | "Unknown secondary PDP context"); |
| 2400 | return EOF; |
| 2401 | } |
| 2402 | if (linked_pdp != secondary_pdp) { |
| 2403 | if (gsn->cb_delete_context) |
| 2404 | gsn->cb_delete_context |
| 2405 | (secondary_pdp); |
| 2406 | pdp_freepdp(secondary_pdp); |
| 2407 | } |
| 2408 | } |
| 2409 | } |
| 2410 | if (gsn->cb_delete_context) |
| 2411 | gsn->cb_delete_context(linked_pdp); |
| 2412 | pdp_freepdp(linked_pdp); |
| 2413 | } else { |
| 2414 | if (gsn->cb_delete_context) |
| 2415 | gsn->cb_delete_context(pdp); |
| 2416 | if (pdp == linked_pdp) { |
| 2417 | linked_pdp->secondary_tei[pdp->nsapi & 0xf0] = 0; |
| 2418 | linked_pdp->nodata = 1; |
| 2419 | } else |
| 2420 | pdp_freepdp(pdp); |
| 2421 | } |
| 2422 | |
| 2423 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2424 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2425 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2426 | /* Send Delete PDP Context Response */ |
| 2427 | int gtp_delete_pdp_resp(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2428 | struct sockaddr_in *peer, int fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2429 | void *pack, unsigned len, |
| 2430 | struct pdp_t *pdp, struct pdp_t *linked_pdp, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2431 | uint8_t cause, int teardown) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2432 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2433 | union gtp_packet packet; |
| 2434 | struct pdp_t *secondary_pdp; |
| 2435 | unsigned int length = |
| 2436 | get_default_gtp(version, GTP_DELETE_PDP_RSP, &packet); |
| 2437 | int n; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2438 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2439 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2440 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2441 | gtp_resp(version, gsn, pdp, &packet, length, peer, fd, |
| 2442 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2443 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2444 | if (cause == GTPCAUSE_ACC_REQ) { |
| 2445 | if ((teardown) || (version == 0)) { /* Remove all contexts */ |
| 2446 | for (n = 0; n < PDP_MAXNSAPI; n++) { |
| 2447 | if (linked_pdp->secondary_tei[n]) { |
| 2448 | if (pdp_getgtp1 |
| 2449 | (&secondary_pdp, |
| 2450 | linked_pdp->secondary_tei[n])) { |
| 2451 | gtp_err(LOG_ERR, __FILE__, |
| 2452 | __LINE__, |
| 2453 | "Unknown secondary PDP context"); |
| 2454 | return EOF; |
| 2455 | } |
| 2456 | if (linked_pdp != secondary_pdp) { |
| 2457 | if (gsn->cb_delete_context) |
| 2458 | gsn->cb_delete_context |
| 2459 | (secondary_pdp); |
| 2460 | pdp_freepdp(secondary_pdp); |
| 2461 | } |
| 2462 | } |
| 2463 | } |
| 2464 | if (gsn->cb_delete_context) |
| 2465 | gsn->cb_delete_context(linked_pdp); |
| 2466 | pdp_freepdp(linked_pdp); |
| 2467 | } else { /* Remove only current context */ |
| 2468 | if (gsn->cb_delete_context) |
| 2469 | gsn->cb_delete_context(pdp); |
| 2470 | if (pdp == linked_pdp) { |
| 2471 | linked_pdp->secondary_tei[pdp->nsapi & 0xf0] = |
| 2472 | 0; |
| 2473 | linked_pdp->nodata = 1; |
| 2474 | } else |
| 2475 | pdp_freepdp(pdp); |
| 2476 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2477 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2478 | /* if (cause == GTPCAUSE_ACC_REQ) */ |
| 2479 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /* Handle Delete PDP Context Request */ |
| 2483 | int gtp_delete_pdp_ind(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2484 | struct sockaddr_in *peer, int fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2485 | void *pack, unsigned len) |
| 2486 | { |
| 2487 | struct pdp_t *pdp = NULL; |
| 2488 | struct pdp_t *linked_pdp = NULL; |
| 2489 | union gtpie_member *ie[GTPIE_SIZE]; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2490 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2491 | uint16_t seq = get_seq(pack); |
| 2492 | int hlen = get_hlen(pack); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2493 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2494 | uint8_t nsapi; |
| 2495 | uint8_t teardown = 0; |
| 2496 | int n; |
| 2497 | int count = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2498 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2499 | /* Is this a dublicate ? */ |
| 2500 | if (!gtp_dublicate(gsn, version, peer, seq)) { |
| 2501 | return 0; /* We allready send off response once */ |
| 2502 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2503 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2504 | /* Find the linked context in question */ |
| 2505 | if (pdp_getgtp1(&linked_pdp, get_tei(pack))) { |
| 2506 | gsn->err_unknownpdp++; |
| 2507 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2508 | "Unknown PDP context"); |
| 2509 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2510 | NULL, NULL, GTPCAUSE_NON_EXIST, |
| 2511 | teardown); |
| 2512 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2513 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2514 | /* If version 0 this is also the secondary context */ |
| 2515 | if (version == 0) |
| 2516 | pdp = linked_pdp; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2517 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2518 | /* Decode information elements */ |
| 2519 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2520 | gsn->invalid++; |
| 2521 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2522 | "Invalid message format"); |
| 2523 | if (0 == version) |
| 2524 | return EOF; |
| 2525 | else |
| 2526 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, |
| 2527 | len, NULL, NULL, |
| 2528 | GTPCAUSE_INVALID_MESSAGE, |
| 2529 | teardown); |
| 2530 | } |
| 2531 | |
| 2532 | if (version == 1) { |
| 2533 | /* NSAPI (mandatory) */ |
| 2534 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) { |
| 2535 | gsn->missing++; |
| 2536 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2537 | len, "Missing mandatory information field"); |
| 2538 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, |
| 2539 | len, NULL, NULL, |
| 2540 | GTPCAUSE_MAN_IE_MISSING, |
| 2541 | teardown); |
| 2542 | } |
| 2543 | |
| 2544 | /* Find the context in question */ |
| 2545 | if (pdp_getgtp1(&pdp, linked_pdp->secondary_tei[nsapi & 0x0f])) { |
| 2546 | gsn->err_unknownpdp++; |
| 2547 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2548 | len, "Unknown PDP context"); |
| 2549 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, |
| 2550 | len, NULL, NULL, |
| 2551 | GTPCAUSE_NON_EXIST, |
| 2552 | teardown); |
| 2553 | } |
| 2554 | |
| 2555 | /* Teardown (conditional) */ |
| 2556 | gtpie_gettv1(ie, GTPIE_TEARDOWN, 0, &teardown); |
| 2557 | |
| 2558 | if (!teardown) { |
| 2559 | for (n = 0; n < PDP_MAXNSAPI; n++) |
| 2560 | if (linked_pdp->secondary_tei[n]) |
| 2561 | count++; |
| 2562 | if (count <= 1) { |
| 2563 | return 0; /* 29.060 7.3.5 Ignore message */ |
| 2564 | } |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2569 | pdp, linked_pdp, GTPCAUSE_ACC_REQ, teardown); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2572 | /* Handle Delete PDP Context Response */ |
| 2573 | int gtp_delete_pdp_conf(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2574 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 2575 | { |
| 2576 | union gtpie_member *ie[GTPIE_SIZE]; |
| 2577 | uint8_t cause; |
| 2578 | void *cbp = NULL; |
| 2579 | uint8_t type = 0; |
| 2580 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2581 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2582 | /* Remove packet from queue */ |
| 2583 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) |
| 2584 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2585 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2586 | /* Decode information elements */ |
| 2587 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2588 | gsn->invalid++; |
| 2589 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2590 | "Invalid message format"); |
| 2591 | if (gsn->cb_conf) |
| 2592 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 2593 | return EOF; |
| 2594 | } |
| 2595 | |
| 2596 | /* Extract cause value (mandatory) */ |
| 2597 | if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) { |
| 2598 | gsn->missing++; |
| 2599 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2600 | "Missing mandatory information field"); |
| 2601 | if (gsn->cb_conf) |
| 2602 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 2603 | return EOF; |
| 2604 | } |
| 2605 | |
| 2606 | /* Check the cause value (again) */ |
| 2607 | if ((GTPCAUSE_ACC_REQ != cause) && (GTPCAUSE_NON_EXIST != cause)) { |
| 2608 | gsn->err_cause++; |
| 2609 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2610 | "Unexpected cause value received: %d", cause); |
| 2611 | if (gsn->cb_conf) |
| 2612 | gsn->cb_conf(type, cause, NULL, cbp); |
| 2613 | return EOF; |
| 2614 | } |
| 2615 | |
| 2616 | /* Callback function to notify application */ |
| 2617 | if (gsn->cb_conf) |
| 2618 | gsn->cb_conf(type, cause, NULL, cbp); |
| 2619 | |
| 2620 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | /* Send Error Indication (response to a GPDU message */ |
| 2624 | int gtp_error_ind_resp(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2625 | struct sockaddr_in *peer, int fd, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2626 | void *pack, unsigned len) |
| 2627 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2628 | union gtp_packet packet; |
| 2629 | unsigned int length = get_default_gtp(version, GTP_ERROR, &packet); |
| 2630 | |
| 2631 | return gtp_resp(version, gsn, NULL, &packet, length, peer, fd, |
| 2632 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | /* Handle Error Indication */ |
| 2636 | int gtp_error_ind_conf(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2637 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 2638 | { |
| 2639 | struct pdp_t *pdp; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2640 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2641 | /* Find the context in question */ |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 2642 | if (pdp_tidget(&pdp, be64toh(((union gtp_packet *)pack)->gtp0.h.tid))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2643 | gsn->err_unknownpdp++; |
| 2644 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2645 | "Unknown PDP context"); |
| 2646 | return EOF; |
| 2647 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2648 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2649 | gsn->err_unknownpdp++; /* TODO: Change counter */ |
| 2650 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2651 | "Received Error Indication"); |
| 2652 | |
| 2653 | if (gsn->cb_delete_context) |
| 2654 | gsn->cb_delete_context(pdp); |
| 2655 | pdp_freepdp(pdp); |
| 2656 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | int gtp_gpdu_ind(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2660 | struct sockaddr_in *peer, int fd, void *pack, unsigned len) |
| 2661 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2662 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2663 | int hlen = GTP1_HEADER_SIZE_SHORT; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2664 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2665 | /* Need to include code to verify packet src and dest addresses */ |
| 2666 | struct pdp_t *pdp; |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2667 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2668 | if (version == 0) { |
| 2669 | if (pdp_getgtp0 |
| 2670 | (&pdp, ntoh16(((union gtp_packet *)pack)->gtp0.h.flow))) { |
| 2671 | gsn->err_unknownpdp++; |
| 2672 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2673 | len, "Unknown PDP context"); |
| 2674 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, |
| 2675 | len); |
| 2676 | } |
| 2677 | hlen = GTP0_HEADER_SIZE; |
| 2678 | } else if (version == 1) { |
| 2679 | if (pdp_getgtp1 |
| 2680 | (&pdp, ntoh32(((union gtp_packet *)pack)->gtp1l.h.tei))) { |
| 2681 | gsn->err_unknownpdp++; |
| 2682 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, |
| 2683 | len, "Unknown PDP context"); |
| 2684 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, |
| 2685 | len); |
| 2686 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2687 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2688 | /* Is this a long or a short header ? */ |
| 2689 | if (((union gtp_packet *)pack)->gtp1l.h.flags & 0x07) |
| 2690 | hlen = GTP1_HEADER_SIZE_LONG; |
| 2691 | else |
| 2692 | hlen = GTP1_HEADER_SIZE_SHORT; |
| 2693 | } else { |
| 2694 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2695 | "Unknown version"); |
| 2696 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2697 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2698 | /* If the GPDU was not from the peer GSN tell him to delete context */ |
| 2699 | if (memcmp(&peer->sin_addr, pdp->gsnru.v, pdp->gsnru.l)) { /* TODO Range? */ |
| 2700 | gsn->err_unknownpdp++; |
| 2701 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, peer, pack, len, |
| 2702 | "Unknown PDP context"); |
| 2703 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, len); |
| 2704 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2705 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2706 | /* Callback function */ |
| 2707 | if (gsn->cb_data_ind != 0) |
| 2708 | return gsn->cb_data_ind(pdp, pack + hlen, len - hlen); |
| 2709 | |
| 2710 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2713 | /* Receives GTP packet and sends off for further processing |
| 2714 | * Function will check the validity of the header. If the header |
| 2715 | * is not valid the packet is either dropped or a version not |
| 2716 | * supported is returned to the peer. |
| 2717 | * TODO: Need to decide on return values! */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2718 | int gtp_decaps0(struct gsn_t *gsn) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2719 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2720 | unsigned char buffer[PACKET_MAX]; |
| 2721 | struct sockaddr_in peer; |
Harald Welte | d88e11d | 2011-11-02 13:09:43 +0100 | [diff] [blame] | 2722 | socklen_t peerlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2723 | int status; |
| 2724 | struct gtp0_header *pheader; |
| 2725 | int version = 0; /* GTP version should be determined from header! */ |
| 2726 | int fd = gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2727 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2728 | /* TODO: Need strategy of userspace buffering and blocking */ |
| 2729 | /* Currently read is non-blocking and send is blocking. */ |
| 2730 | /* This means that the program have to wait for busy send calls... */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2731 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2732 | while (1) { /* Loop until no more to read */ |
| 2733 | if (fcntl(gsn->fd0, F_SETFL, O_NONBLOCK)) { |
| 2734 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 2735 | return -1; |
| 2736 | } |
| 2737 | peerlen = sizeof(peer); |
| 2738 | if ((status = |
| 2739 | recvfrom(gsn->fd0, buffer, sizeof(buffer), 0, |
| 2740 | (struct sockaddr *)&peer, &peerlen)) < 0) { |
| 2741 | if (errno == EAGAIN) |
| 2742 | return 0; |
| 2743 | gsn->err_readfrom++; |
| 2744 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 2745 | "recvfrom(fd0=%d, buffer=%lx, len=%d) failed: status = %d error = %s", |
| 2746 | gsn->fd0, (unsigned long)buffer, sizeof(buffer), |
| 2747 | status, status ? strerror(errno) : "No error"); |
| 2748 | return -1; |
| 2749 | } |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2750 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2751 | /* Need at least 1 byte in order to check version */ |
| 2752 | if (status < (1)) { |
| 2753 | gsn->empty++; |
| 2754 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2755 | status, "Discarding packet - too small"); |
| 2756 | continue; |
| 2757 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2758 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2759 | pheader = (struct gtp0_header *)(buffer); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2760 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2761 | /* Version should be gtp0 (or earlier) */ |
| 2762 | /* 09.60 is somewhat unclear on this issue. On gsn->fd0 we expect only */ |
| 2763 | /* GTP 0 messages. If other version message is received we reply that we */ |
| 2764 | /* only support version 0, implying that this is the only version */ |
| 2765 | /* supported on this port */ |
| 2766 | if (((pheader->flags & 0xe0) > 0x00)) { |
| 2767 | gsn->unsup++; |
| 2768 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2769 | status, "Unsupported GTP version"); |
| 2770 | gtp_unsup_req(gsn, 0, &peer, gsn->fd0, buffer, status); /* 29.60: 11.1.1 */ |
| 2771 | continue; |
| 2772 | } |
| 2773 | |
| 2774 | /* Check length of gtp0 packet */ |
| 2775 | if (status < GTP0_HEADER_SIZE) { |
| 2776 | gsn->tooshort++; |
| 2777 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2778 | status, "GTP0 packet too short"); |
| 2779 | continue; /* Silently discard 29.60: 11.1.2 */ |
| 2780 | } |
| 2781 | |
| 2782 | /* Check packet length field versus length of packet */ |
| 2783 | if (status != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) { |
| 2784 | gsn->tooshort++; |
| 2785 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2786 | status, |
| 2787 | "GTP packet length field does not match actual length"); |
| 2788 | continue; /* Silently discard */ |
| 2789 | } |
| 2790 | |
| 2791 | if ((gsn->mode == GTP_MODE_GGSN) && |
| 2792 | ((pheader->type == GTP_CREATE_PDP_RSP) || |
| 2793 | (pheader->type == GTP_UPDATE_PDP_RSP) || |
| 2794 | (pheader->type == GTP_DELETE_PDP_RSP))) { |
| 2795 | gsn->unexpect++; |
| 2796 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2797 | status, |
| 2798 | "Unexpected GTP Signalling Message"); |
| 2799 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 2800 | } |
| 2801 | |
| 2802 | if ((gsn->mode == GTP_MODE_SGSN) && |
| 2803 | ((pheader->type == GTP_CREATE_PDP_REQ) || |
| 2804 | (pheader->type == GTP_UPDATE_PDP_REQ) || |
| 2805 | (pheader->type == GTP_DELETE_PDP_REQ))) { |
| 2806 | gsn->unexpect++; |
| 2807 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2808 | status, |
| 2809 | "Unexpected GTP Signalling Message"); |
| 2810 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 2811 | } |
| 2812 | |
| 2813 | switch (pheader->type) { |
| 2814 | case GTP_ECHO_REQ: |
| 2815 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
| 2816 | break; |
| 2817 | case GTP_ECHO_RSP: |
| 2818 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
| 2819 | break; |
| 2820 | case GTP_NOT_SUPPORTED: |
| 2821 | gtp_unsup_ind(gsn, &peer, buffer, status); |
| 2822 | break; |
| 2823 | case GTP_CREATE_PDP_REQ: |
| 2824 | gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, |
| 2825 | status); |
| 2826 | break; |
| 2827 | case GTP_CREATE_PDP_RSP: |
| 2828 | gtp_create_pdp_conf(gsn, version, &peer, buffer, |
| 2829 | status); |
| 2830 | break; |
| 2831 | case GTP_UPDATE_PDP_REQ: |
| 2832 | gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, |
| 2833 | status); |
| 2834 | break; |
| 2835 | case GTP_UPDATE_PDP_RSP: |
| 2836 | gtp_update_pdp_conf(gsn, version, &peer, buffer, |
| 2837 | status); |
| 2838 | break; |
| 2839 | case GTP_DELETE_PDP_REQ: |
| 2840 | gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, |
| 2841 | status); |
| 2842 | break; |
| 2843 | case GTP_DELETE_PDP_RSP: |
| 2844 | gtp_delete_pdp_conf(gsn, version, &peer, buffer, |
| 2845 | status); |
| 2846 | break; |
| 2847 | case GTP_ERROR: |
| 2848 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
| 2849 | break; |
| 2850 | case GTP_GPDU: |
| 2851 | gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status); |
| 2852 | break; |
| 2853 | default: |
| 2854 | gsn->unknown++; |
| 2855 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2856 | status, |
| 2857 | "Unknown GTP message type received"); |
| 2858 | break; |
| 2859 | } |
| 2860 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2863 | int gtp_decaps1c(struct gsn_t *gsn) |
| 2864 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2865 | unsigned char buffer[PACKET_MAX]; |
| 2866 | struct sockaddr_in peer; |
Harald Welte | d88e11d | 2011-11-02 13:09:43 +0100 | [diff] [blame] | 2867 | socklen_t peerlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2868 | int status; |
| 2869 | struct gtp1_header_short *pheader; |
| 2870 | int version = 1; /* TODO GTP version should be determined from header! */ |
| 2871 | int fd = gsn->fd1c; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2872 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2873 | /* TODO: Need strategy of userspace buffering and blocking */ |
| 2874 | /* Currently read is non-blocking and send is blocking. */ |
| 2875 | /* This means that the program have to wait for busy send calls... */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2876 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2877 | while (1) { /* Loop until no more to read */ |
| 2878 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
| 2879 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 2880 | return -1; |
| 2881 | } |
| 2882 | peerlen = sizeof(peer); |
| 2883 | if ((status = |
| 2884 | recvfrom(fd, buffer, sizeof(buffer), 0, |
| 2885 | (struct sockaddr *)&peer, &peerlen)) < 0) { |
| 2886 | if (errno == EAGAIN) |
| 2887 | return 0; |
| 2888 | gsn->err_readfrom++; |
| 2889 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 2890 | "recvfrom(fd=%d, buffer=%lx, len=%d) failed: status = %d error = %s", |
| 2891 | fd, (unsigned long)buffer, sizeof(buffer), |
| 2892 | status, status ? strerror(errno) : "No error"); |
| 2893 | return -1; |
| 2894 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2895 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2896 | /* Need at least 1 byte in order to check version */ |
| 2897 | if (status < (1)) { |
| 2898 | gsn->empty++; |
| 2899 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2900 | status, "Discarding packet - too small"); |
| 2901 | continue; |
| 2902 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2903 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2904 | pheader = (struct gtp1_header_short *)(buffer); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2905 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2906 | /* Version must be no larger than GTP 1 */ |
| 2907 | if (((pheader->flags & 0xe0) > 0x20)) { |
| 2908 | gsn->unsup++; |
| 2909 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2910 | status, "Unsupported GTP version"); |
| 2911 | gtp_unsup_req(gsn, version, &peer, fd, buffer, status); |
| 2912 | /*29.60: 11.1.1 */ |
| 2913 | continue; |
| 2914 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2915 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2916 | /* Version must be at least GTP 1 */ |
| 2917 | /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */ |
| 2918 | /* GTP 1 messages. If GTP 0 message is received we silently discard */ |
| 2919 | /* the message */ |
| 2920 | if (((pheader->flags & 0xe0) < 0x20)) { |
| 2921 | gsn->unsup++; |
| 2922 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2923 | status, "Unsupported GTP version"); |
| 2924 | continue; |
| 2925 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2926 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2927 | /* Check packet flag field */ |
| 2928 | if (((pheader->flags & 0xf7) != 0x32)) { |
| 2929 | gsn->unsup++; |
| 2930 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2931 | status, "Unsupported packet flag"); |
| 2932 | continue; |
| 2933 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2934 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2935 | /* Check length of packet */ |
| 2936 | if (status < GTP1_HEADER_SIZE_LONG) { |
| 2937 | gsn->tooshort++; |
| 2938 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2939 | status, "GTP packet too short"); |
| 2940 | continue; /* Silently discard 29.60: 11.1.2 */ |
| 2941 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2942 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2943 | /* Check packet length field versus length of packet */ |
| 2944 | if (status != |
| 2945 | (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) { |
| 2946 | gsn->tooshort++; |
| 2947 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2948 | status, |
| 2949 | "GTP packet length field does not match actual length"); |
| 2950 | continue; /* Silently discard */ |
| 2951 | } |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2952 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2953 | /* Check for extension headers */ |
| 2954 | /* TODO: We really should cycle through the headers and determine */ |
| 2955 | /* if any have the comprehension required flag set */ |
| 2956 | if (((pheader->flags & 0x04) != 0x00)) { |
| 2957 | gsn->unsup++; |
| 2958 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2959 | status, "Unsupported extension header"); |
| 2960 | gtp_extheader_req(gsn, version, &peer, fd, buffer, |
| 2961 | status); |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2962 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2963 | continue; |
| 2964 | } |
| 2965 | |
| 2966 | if ((gsn->mode == GTP_MODE_GGSN) && |
| 2967 | ((pheader->type == GTP_CREATE_PDP_RSP) || |
| 2968 | (pheader->type == GTP_UPDATE_PDP_RSP) || |
| 2969 | (pheader->type == GTP_DELETE_PDP_RSP))) { |
| 2970 | gsn->unexpect++; |
| 2971 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2972 | status, |
| 2973 | "Unexpected GTP Signalling Message"); |
| 2974 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 2975 | } |
| 2976 | |
| 2977 | if ((gsn->mode == GTP_MODE_SGSN) && |
| 2978 | ((pheader->type == GTP_CREATE_PDP_REQ) || |
| 2979 | (pheader->type == GTP_UPDATE_PDP_REQ) || |
| 2980 | (pheader->type == GTP_DELETE_PDP_REQ))) { |
| 2981 | gsn->unexpect++; |
| 2982 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 2983 | status, |
| 2984 | "Unexpected GTP Signalling Message"); |
| 2985 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 2986 | } |
| 2987 | |
| 2988 | switch (pheader->type) { |
| 2989 | case GTP_ECHO_REQ: |
| 2990 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
| 2991 | break; |
| 2992 | case GTP_ECHO_RSP: |
| 2993 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
| 2994 | break; |
| 2995 | case GTP_NOT_SUPPORTED: |
| 2996 | gtp_unsup_ind(gsn, &peer, buffer, status); |
| 2997 | break; |
| 2998 | case GTP_SUPP_EXT_HEADER: |
| 2999 | gtp_extheader_ind(gsn, &peer, buffer, status); |
| 3000 | break; |
| 3001 | case GTP_CREATE_PDP_REQ: |
| 3002 | gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3003 | status); |
| 3004 | break; |
| 3005 | case GTP_CREATE_PDP_RSP: |
| 3006 | gtp_create_pdp_conf(gsn, version, &peer, buffer, |
| 3007 | status); |
| 3008 | break; |
| 3009 | case GTP_UPDATE_PDP_REQ: |
| 3010 | gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3011 | status); |
| 3012 | break; |
| 3013 | case GTP_UPDATE_PDP_RSP: |
| 3014 | gtp_update_pdp_conf(gsn, version, &peer, buffer, |
| 3015 | status); |
| 3016 | break; |
| 3017 | case GTP_DELETE_PDP_REQ: |
| 3018 | gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3019 | status); |
| 3020 | break; |
| 3021 | case GTP_DELETE_PDP_RSP: |
| 3022 | gtp_delete_pdp_conf(gsn, version, &peer, buffer, |
| 3023 | status); |
| 3024 | break; |
| 3025 | case GTP_ERROR: |
| 3026 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
| 3027 | break; |
| 3028 | default: |
| 3029 | gsn->unknown++; |
| 3030 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 3031 | status, |
| 3032 | "Unknown GTP message type received"); |
| 3033 | break; |
| 3034 | } |
| 3035 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3038 | int gtp_decaps1u(struct gsn_t *gsn) |
| 3039 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3040 | unsigned char buffer[PACKET_MAX]; |
| 3041 | struct sockaddr_in peer; |
Harald Welte | d88e11d | 2011-11-02 13:09:43 +0100 | [diff] [blame] | 3042 | socklen_t peerlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3043 | int status; |
| 3044 | struct gtp1_header_short *pheader; |
| 3045 | int version = 1; /* GTP version should be determined from header! */ |
| 3046 | int fd = gsn->fd1u; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3047 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3048 | /* TODO: Need strategy of userspace buffering and blocking */ |
| 3049 | /* Currently read is non-blocking and send is blocking. */ |
| 3050 | /* This means that the program have to wait for busy send calls... */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3051 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3052 | while (1) { /* Loop until no more to read */ |
| 3053 | if (fcntl(gsn->fd1u, F_SETFL, O_NONBLOCK)) { |
| 3054 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 3055 | return -1; |
| 3056 | } |
| 3057 | peerlen = sizeof(peer); |
| 3058 | if ((status = |
| 3059 | recvfrom(gsn->fd1u, buffer, sizeof(buffer), 0, |
| 3060 | (struct sockaddr *)&peer, &peerlen)) < 0) { |
| 3061 | if (errno == EAGAIN) |
| 3062 | return 0; |
| 3063 | gsn->err_readfrom++; |
| 3064 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 3065 | "recvfrom(fd1u=%d, buffer=%lx, len=%d) failed: status = %d error = %s", |
| 3066 | gsn->fd1u, (unsigned long)buffer, |
| 3067 | sizeof(buffer), status, |
| 3068 | status ? strerror(errno) : "No error"); |
| 3069 | return -1; |
| 3070 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3071 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3072 | /* Need at least 1 byte in order to check version */ |
| 3073 | if (status < (1)) { |
| 3074 | gsn->empty++; |
| 3075 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 3076 | status, "Discarding packet - too small"); |
| 3077 | continue; |
| 3078 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3079 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3080 | pheader = (struct gtp1_header_short *)(buffer); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3081 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3082 | /* Version must be no larger than GTP 1 */ |
| 3083 | if (((pheader->flags & 0xe0) > 0x20)) { |
| 3084 | gsn->unsup++; |
| 3085 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 3086 | status, "Unsupported GTP version"); |
| 3087 | gtp_unsup_req(gsn, 1, &peer, gsn->fd1c, buffer, status); /*29.60: 11.1.1 */ |
| 3088 | continue; |
| 3089 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3090 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3091 | /* Version must be at least GTP 1 */ |
| 3092 | /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */ |
| 3093 | /* GTP 1 messages. If GTP 0 message is received we silently discard */ |
| 3094 | /* the message */ |
| 3095 | if (((pheader->flags & 0xe0) < 0x20)) { |
| 3096 | gsn->unsup++; |
| 3097 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 3098 | status, "Unsupported GTP version"); |
| 3099 | continue; |
| 3100 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3101 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3102 | /* Check packet flag field (allow both with and without sequence number) */ |
| 3103 | if (((pheader->flags & 0xf5) != 0x30)) { |
| 3104 | gsn->unsup++; |
| 3105 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 3106 | status, "Unsupported packet flag"); |
| 3107 | continue; |
| 3108 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3109 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3110 | /* Check length of packet */ |
| 3111 | if (status < GTP1_HEADER_SIZE_SHORT) { |
| 3112 | gsn->tooshort++; |
| 3113 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 3114 | status, "GTP packet too short"); |
| 3115 | continue; /* Silently discard 29.60: 11.1.2 */ |
| 3116 | } |
| 3117 | |
| 3118 | /* Check packet length field versus length of packet */ |
| 3119 | if (status != |
| 3120 | (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) { |
| 3121 | gsn->tooshort++; |
| 3122 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 3123 | status, |
| 3124 | "GTP packet length field does not match actual length"); |
| 3125 | continue; /* Silently discard */ |
| 3126 | } |
| 3127 | |
| 3128 | /* Check for extension headers */ |
| 3129 | /* TODO: We really should cycle through the headers and determine */ |
| 3130 | /* if any have the comprehension required flag set */ |
| 3131 | if (((pheader->flags & 0x04) != 0x00)) { |
| 3132 | gsn->unsup++; |
| 3133 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 3134 | status, "Unsupported extension header"); |
| 3135 | gtp_extheader_req(gsn, version, &peer, fd, buffer, |
| 3136 | status); |
| 3137 | |
| 3138 | continue; |
| 3139 | } |
| 3140 | |
| 3141 | switch (pheader->type) { |
| 3142 | case GTP_ECHO_REQ: |
| 3143 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
| 3144 | break; |
| 3145 | case GTP_ECHO_RSP: |
| 3146 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
| 3147 | break; |
| 3148 | case GTP_SUPP_EXT_HEADER: |
| 3149 | gtp_extheader_ind(gsn, &peer, buffer, status); |
| 3150 | break; |
| 3151 | case GTP_ERROR: |
| 3152 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
| 3153 | break; |
| 3154 | /* Supported header extensions */ |
| 3155 | case GTP_GPDU: |
| 3156 | gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status); |
| 3157 | break; |
| 3158 | default: |
| 3159 | gsn->unknown++; |
| 3160 | gtp_errpack(LOG_ERR, __FILE__, __LINE__, &peer, buffer, |
| 3161 | status, |
| 3162 | "Unknown GTP message type received"); |
| 3163 | break; |
| 3164 | } |
| 3165 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3168 | int gtp_data_req(struct gsn_t *gsn, struct pdp_t *pdp, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3169 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3170 | union gtp_packet packet; |
| 3171 | struct sockaddr_in addr; |
| 3172 | int fd; |
| 3173 | int length; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3174 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3175 | memset(&addr, 0, sizeof(addr)); |
| 3176 | addr.sin_family = AF_INET; |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3177 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3178 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 3179 | #endif |
| 3180 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3181 | memcpy(&addr.sin_addr, pdp->gsnru.v, pdp->gsnru.l); /* TODO range check */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3182 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3183 | if (pdp->version == 0) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3184 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3185 | length = GTP0_HEADER_SIZE + len; |
| 3186 | addr.sin_port = htons(GTP0_PORT); |
| 3187 | fd = gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3188 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3189 | get_default_gtp(0, GTP_GPDU, &packet); |
| 3190 | packet.gtp0.h.length = hton16(len); |
| 3191 | packet.gtp0.h.seq = hton16(pdp->gtpsntx++); |
| 3192 | packet.gtp0.h.flow = hton16(pdp->flru); |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame^] | 3193 | packet.gtp0.h.tid = htobe64(pdp_gettid(pdp->imsi, pdp->nsapi)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3194 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3195 | if (len > sizeof(union gtp_packet) - sizeof(struct gtp0_header)) { |
| 3196 | gsn->err_memcpy++; |
| 3197 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 3198 | "Memcpy failed: %d > %d", len, |
| 3199 | sizeof(union gtp_packet) - |
| 3200 | sizeof(struct gtp0_header)); |
| 3201 | return EOF; |
| 3202 | } |
| 3203 | memcpy(packet.gtp0.p, pack, len); /* TODO Should be avoided! */ |
| 3204 | } else if (pdp->version == 1) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3205 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3206 | length = GTP1_HEADER_SIZE_LONG + len; |
| 3207 | addr.sin_port = htons(GTP1U_PORT); |
| 3208 | fd = gsn->fd1u; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 3209 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3210 | get_default_gtp(1, GTP_GPDU, &packet); |
| 3211 | packet.gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT + |
| 3212 | GTP1_HEADER_SIZE_LONG); |
| 3213 | packet.gtp1l.h.seq = hton16(pdp->gtpsntx++); |
| 3214 | packet.gtp1l.h.tei = hton32(pdp->teid_gn); |
| 3215 | |
| 3216 | if (len > |
| 3217 | sizeof(union gtp_packet) - |
| 3218 | sizeof(struct gtp1_header_long)) { |
| 3219 | gsn->err_memcpy++; |
| 3220 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 3221 | "Memcpy failed: %d > %d", len, |
| 3222 | sizeof(union gtp_packet) - |
| 3223 | sizeof(struct gtp0_header)); |
| 3224 | return EOF; |
| 3225 | } |
| 3226 | memcpy(packet.gtp1l.p, pack, len); /* TODO Should be avoided! */ |
| 3227 | } else { |
| 3228 | gtp_err(LOG_ERR, __FILE__, __LINE__, "Unknown version"); |
| 3229 | return EOF; |
| 3230 | } |
| 3231 | |
| 3232 | if (fcntl(fd, F_SETFL, 0)) { |
| 3233 | gtp_err(LOG_ERR, __FILE__, __LINE__, "fnctl()"); |
| 3234 | return -1; |
| 3235 | } |
| 3236 | |
| 3237 | if (sendto(fd, &packet, length, 0, |
| 3238 | (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 3239 | gsn->err_sendto++; |
| 3240 | gtp_err(LOG_ERR, __FILE__, __LINE__, |
| 3241 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s", fd, |
| 3242 | (unsigned long)&packet, GTP0_HEADER_SIZE + len, |
| 3243 | strerror(errno)); |
| 3244 | return EOF; |
| 3245 | } |
| 3246 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3249 | /* *********************************************************** |
| 3250 | * Conversion functions |
| 3251 | *************************************************************/ |
| 3252 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3253 | int char2ul_t(char *src, struct ul_t dst) |
| 3254 | { |
| 3255 | dst.l = strlen(src) + 1; |
| 3256 | dst.v = malloc(dst.l); |
| 3257 | dst.v[0] = dst.l - 1; |
| 3258 | memcpy(&dst.v[1], src, dst.v[0]); |
| 3259 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | /* *********************************************************** |
| 3263 | * IP address conversion functions |
| 3264 | * There exist several types of address representations: |
| 3265 | * - eua: End User Address. (29.060, 7.7.27, message type 128) |
| 3266 | * Used for signalling address to mobile station. Supports IPv4 |
| 3267 | * IPv6 x.25 etc. etc. |
| 3268 | * - gsna: GSN Address. (29.060, 7.7.32, message type 133): IP address |
| 3269 | * of GSN. If length is 4 it is IPv4. If length is 16 it is IPv6. |
| 3270 | * - in_addr: IPv4 address struct. |
| 3271 | * - sockaddr_in: Socket API representation of IP address and |
| 3272 | * port number. |
| 3273 | *************************************************************/ |
| 3274 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3275 | int ipv42eua(struct ul66_t *eua, struct in_addr *src) |
| 3276 | { |
| 3277 | eua->v[0] = 0xf1; /* IETF */ |
| 3278 | eua->v[1] = 0x21; /* IPv4 */ |
| 3279 | if (src) { |
| 3280 | eua->l = 6; |
| 3281 | memcpy(&eua->v[2], src, 4); |
| 3282 | } else { |
| 3283 | eua->l = 2; |
| 3284 | } |
| 3285 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3288 | int eua2ipv4(struct in_addr *dst, struct ul66_t *eua) |
| 3289 | { |
| 3290 | if ((eua->l != 6) || (eua->v[0] != 0xf1) || (eua->v[1] = 0x21)) |
| 3291 | return -1; /* Not IPv4 address */ |
| 3292 | memcpy(dst, &eua->v[2], 4); |
| 3293 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3296 | int gsna2in_addr(struct in_addr *dst, struct ul16_t *gsna) |
| 3297 | { |
| 3298 | memset(dst, 0, sizeof(struct in_addr)); |
| 3299 | if (gsna->l != 4) |
| 3300 | return EOF; /* Return if not IPv4 */ |
| 3301 | memcpy(dst, gsna->v, gsna->l); |
| 3302 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3305 | int in_addr2gsna(struct ul16_t *gsna, struct in_addr *src) |
| 3306 | { |
| 3307 | memset(gsna, 0, sizeof(struct ul16_t)); |
| 3308 | gsna->l = 4; |
| 3309 | memcpy(gsna->v, src, gsna->l); |
| 3310 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3311 | } |