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